来自数据库数组的 PHP TreeView

标签 php mysql arrays treeview

我从数据库查询中得到以下数组。
我想创建一个 TreeView ,其中父级是“win_name”,子级是“item_name”。在过去的几天里,我尝试了 foreach 和 while 循环的组合、本论坛中的其他答案、在线教程、jquery 插件和 jstree。我似乎无法让它发挥作用

父=“win_name”
--child =“项目名称”

array(857) {
     [0]=>
     object(stdClass)#11 (5) {
       ["item_name"]=>
       string(6) "$rocks"
       ["item_id"]=>
       string(4) "3045"
       ["win_name"]=>
       string(12) "Tequila Mods"
       ["win_id"]=>
       string(1) "3"
       ["sales_mode"]=>
       string(1) "1"
     }
     [1]=>
     object(stdClass)#24 (5) {
       ["item_name"]=>
       string(13) "Queso Fundido"
       ["item_id"]=>
       string(4) "1101"
       ["win_name"]=>
       string(6) "Snacks"
       ["win_id"]=>
       string(2) "29"
       ["sales_mode"]=>
       string(1) "1"
     }
     [2]=>
     object(stdClass)#25 (5) {
       ["item_name"]=>
       string(9) "Texaz Dip"
       ["item_id"]=>
       string(4) "1102"
       ["win_name"]=>
       string(6) "Snacks"
       ["win_id"]=>
       string(2) "29"
       ["sales_mode"]=>
       string(1) "1"
     }
     [3]=>
     object(stdClass)#26 (5) {
       ["item_name"]=>
       string(10) "Summer Sea"
       ["item_id"]=>
       string(4) "1481"
       ["win_name"]=>
       string(6) "Snacks"
       ["win_id"]=>
       string(2) "29"
       ["sales_mode"]=>
       string(1) "1"
     }

最佳答案

<?php

class treeView
{
    public $treeview = array();

    // In the construct, we transform your current array to an array that we can use
    function __construct($main)
    {
        $main = (array) $main;

        foreach($main as $view)
        {
            // We add the parent ONLY if we don't already have it
            $this->treeview['parent'][$view['win_name']]['win_name'] = $view['win_name'];
            $this->treeview['parent'][$view['win_name']]['win_id'] = $view['win_id'];
            $this->treeview['parent'][$view['win_name']]['sales_mode'] = $view['sales_mode'];

            // Adding all of the childs
            $this->treeview['child'][$view['win_name']][$view['item_id']]['item_name'] = $view['item_name'];
            $this->treeview['child'][$view['win_name']][$view['item_id']]['item_id'] = $view['item_id'];
        }
    }

    public function buildTree()
    {
        // Looping through each parent and outputting it's content and it's children
        foreach($this->treeview['parent'] as $branch)
        {
            if(isset($this->treeview['child'][$branch['win_name']]))
            {
                echo "<ol>";
                echo "<li>Win Name: {$branch['win_name']}</li>";
                echo "<li>Win ID: {$branch['win_id']}</li>";
                echo "<li>Sales Mode: {$branch['sales_mode']}</li>";
                $this->buildChild($branch['win_name']);         // Our call which builds the children
                echo "</ol>";
            }
            else
            {
                echo "<ol>";
                echo "<li>Win Name: {$branch['win_name']}</li>";
                echo "<li>Win ID: {$branch['win_id']}</li>";
                echo "<li>Sales Mode: {$branch['sales_mode']}</li>";
                echo "</ol>";
            }
        }
    }

    private function buildChild($parent)
    {
        // Looping through each child... technically we could make this recursive and allow children to have children
        foreach($this->treeview['child'][$parent] as $child)
        {
            echo "<li style=\"list-style-type: none;\">";
            echo "<ol>";
            echo "<li>Item Name: {$child['item_name']}</li>";
            echo "<li>Item ID: {$child['item_id']}</li>";
            echo "</ol>";
            echo "</li>";
        }
    }
}

$array = array (
    array (
        'item_name' => "\$rocks",
        'item_id' => "3045",
        'win_name' => "Tequila Mods",
        'win_id' => "3",
        'sales_mode' => "1",
    ),
    array (
        'item_name' => "Queso Fundido",
        'item_id' => "1101",
        'win_name' => "Snacks",
        'win_id' => "29",
        'sales_mode' => "1",
    ),
    array (
        'item_name' => "Texaz Dip",
        'item_id' => "1102",
        'win_name' => "Snacks",
        'win_id' => "29",
        'sales_mode' => "1",
    ),
    array (
        'item_name' => "Summer Sea",
        'item_id' => "1481",
        'win_name' => "Snacks",
        'win_id' => "29",
        'sales_mode' => "1",
    )
);

// Initiating our class
$tree = new treeView($array);

// Building our tree
$tree->buildTree();

?>

这将生成以下列表

<ol><li>Win Name: Tequila Mods</li><li>Win ID: 3</li><li>Sales Mode: 1</li><li style="list-style-type: none;"><ol><li>Item Name: $rocks</li><li>Item ID: 3045</li></ol></li></ol><ol><li>Win Name: Snacks</li><li>Win ID: 29</li><li>Sales Mode: 1</li><li style="list-style-type: none;"><ol><li>Item Name: Queso Fundido</li><li>Item ID: 1101</li></ol></li><li style="list-style-type: none;"><ol><li>Item Name: Texaz Dip</li><li>Item ID: 1102</li></ol></li><li style="list-style-type: none;"><ol><li>Item Name: Summer Sea</li><li>Item ID: 1481</li></ol></li></ol>

说实话,这很脏很丑。我编写了这段代码来与您的数组一起使用,但这不是最好的方法。更好的方法是使用 PDO,并使用 FETCH_GROUP 按 win_name(父级) 进行分组,并以这种方式获取所需的数组。

学习也不错Veerendra's solution谢谢 ficuscr”,并可能尝试根据您的要求实现类似的系统。

关于来自数据库数组的 PHP TreeView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49184120/

相关文章:

php - 插入具有特定计算值的多行

arrays - 如何按作为结构​​值包含的数组进行过滤

java - 理解用随机数填充数组 (Big Java Ex 7.4)

java - 更改字符串以匹配 char 数组

php - 如何显示设定日期后 2 周的日期?

php - 仅在没有引用时引用复合键的一个字段和 'ON DELETE CASCADE'

php - 在 xampp 中调用未定义的函数 sqlsrv_connect()

php - 从 PHP 端处理 MySQL 事务 : strategy and best practices

mySQL:物理排序

php - 选择定义了其他列的 mysql 列(即数组),:- and insert it into php variable