如何使用php显示类似WordPress的类别结构?
Array
([0] => stdClass对象([cat_id] => 64 [名称] =>沐浴皂[slug] =>沐浴皂[cat_taxonomy_id] => 65 [分类学] => product_cat [parent] => 63)
[1] => stdClass Object
(
[cat_id] => 65
[name] => Chemical
[slug] => chemical
[cat_taxonomy_id] => 66
[taxonomy] => product_cat
[parent] => 64
)
[2] => stdClass Object
(
[cat_id] => 63
[name] => Soap
[slug] => soap
[cat_taxonomy_id] => 64
[taxonomy] => product_cat
[parent] => 0
)
)
2 回答
在您的示例中是将数据存储在数据库中的数组 .
对于输出树结构,您应该将其转换为树结构 .
例如:
更改数组并使用cat_id作为主数组中的键 .
添加到每个项目字段childs = array();并存储在此数组ID中的子类别 .
查找根类别(其中parent == 0)并在子项字段中将ID保存为具有键"0"的项目 .
从数据库加载数据时可以执行的步骤1 . 步骤2和3可以在一次迭代中进行(foreach)
在此之后,您的示例将类似
然后你可以输出树 . 只需从key = 0开始并输出所有子项 . 对于每个孩子的首次出局,然后是所有孩子 . 你应该使用递归函数 .
我试过以下链接 . 你也可以试试这个链接
http://stevenbuick.com/category-hierarchy-with-codeigniter-and-jstree/