简体   繁体   English

如何使用php创建像WordPress类别的层次结构

[英]How to create hierarchical structure like WordPress category using php

在此输入图像描述

How to display the category structure like WordPress using php? 如何使用php显示类似WordPress的类别结构?

Array

( [0] => stdClass Object ( [cat_id] => 64 [name] => Bathing Soap [slug] => bathing-soap [cat_taxonomy_id] => 65 [taxonomy] => product_cat [parent] => 63 ) ([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
    )

)

In your example is arrays as you store this records in database. 在您的示例中是将数据存储在数据库中的数组。

For output tree structure you should convert it to tree structure. 对于输出树结构,您应该将其转换为树结构。

For example: 例如:

  1. Change array and use cat_id as key in main array. 更改数组并使用cat_id作为主数组中的键。
  2. Add to each item field childs = array(); 添加到每个项目字段childs = array(); And store in this array ids for childs category. 并存储在子类别的数组ID中。
  3. Find root categories (where parent == 0) and save ids in childs field for item with key "0". 查找根类别(其中parent == 0)并在子项字段中将ID保存为具有键“0”的项目。

Step 1 you can make when you load data from fro database. 从数据库加载数据时可以执行的步骤1。 Steps 2 and 3 can make in one iteration (foreach) 步骤2和3可以在一次迭代中进行(foreach)

After this your example will similar 在此之后,您的示例将类似

array(
  [0] => stdClass Object
    (
      [cat_id] => 0,
      [childs] => array( [0]=>63 )
      ...
    )
  [63]=> stdClass Object
    (
      [cat_id] => 63,
      [childs] => array( [0]=>64 )
      ...
    ) 

Then you can output tree. 然后你可以输出树。 Just begin with key = 0 and output all childs. 只需从key = 0开始并输出所有子项。 For each child in first out title and then all childs. 对于每个孩子的头一个冠军,然后是所有孩子。 You should use recursion function. 你应该使用递归函数。

I have tried this below link. 我试过以下链接。 you can also try this below Link 你也可以尝试下面链接

http://stevenbuick.com/category-hierarchy-with-codeigniter-and-jstree/ http://stevenbuick.com/category-hierarchy-with-codeigniter-and-jstree/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM