如何递归显示多级分类信息

场景:

在做具有多级分类信息内容时(如产品分类、文章分类等),按等级递归递进显示。

效果图:

屏幕快照 2015-05-24 下午3.27.46

【参考信息:】

http://blog.ideashower.com/post/15147134343/create-a-parent-child-array-structure-in-one-pass

http://stackoverflow.com/questions/4452472/category-hierarchy-php-mysql

相关原文:

url_1:   http://stackoverflow.com/questions/4452472/category-hierarchy-php-mysql

问题:

I am trying to get my all categories and sub-categories from MySQL database in a hierarchy:

My result should be like that (just example):

  1. Cat A
    • Sub-Cat 1
      • Sub_Sub_Cat 1
      • Sub_Sub_Cat 2
    • Sub_Cat 2
  2. Cat B
  3. Cat C

MySQL code:

 

Simply, how can get it in a hirarchy with PHP codes?

解答:

When using an adjacency list model, you can generate the structure in one pass.

Taken from One Pass Parent-Child Array Structure (Sep 2007; by Nate Weiner):

 

From the linked article, here’s a snippet to create a list for output. It is recursive, if there a children for a node, it calls itself again to build up the subtree.

 

url_2: http://blog.ideashower.com/post/15147134343/create-a-parent-child-array-structure-in-one-pass
相关内容:

One Pass Parent-Child Array Structure

During the time that I’ve been programming I’ve continually run into times when I needed to be able to store a multi-level list into a database and retrieve it. I’ve used this in a CMS system to allow uses to create and nest their own menu options or to allow users to create and nest categories of items. Saving it was never a problem, but retrieving it and building a PHP array with nested structure was a messy task.

We want to turn this:

Database Table: Items
Id Parent Id Name
1 0 North America
2 1 Canada
3 7 California
4 0 Europe
5 0 Antartica
6 3 Los Angeles
7 1 United States
8 7 Florida
9 3 Hollywood
10 2 Quebec

Into this:

  • Antartica
  • North America
    • Canada
      • Quebec
    • United States
      • California
        • Hollywood
        • Los Angeles
    • Florida
  • South America

The problem was that you couldn’t easily enter a variable four levels deep into an array without knowing all of the parents of that item. This was okay if you retrieved the data in order of parents, but that meant if you wanted to sort the data by another column (such as name), you would have to do multiple passes through your data to continully reshuffle it. And this is a huge drain of cpu cycles on even medium sized data-sets.

The Solution

Let me show you what it looks like:


 

How it Works

PHP REFERENCES

This solution’s core is based on using PHP References. A reference allows you to access the same variable but with different names. In other words, it allows you to assign aliases to a variable. This way, any changes made to the reference will also affect the original variable. Here’s an example:

$var = ‘Hello World’; $ref = &$var; //you define a reference by using ’&’ $ref = ‘Goodbye World’; print “var = ” . $var; print “ref = ” . $ref;Outputs: var = Goodbye World ref = Goodbye World

TWO LISTS

To solve this, we are going to use not one, but two arrays. We will have our main array which is the one we are building with multilple levels, and secondly, we will have a single-level array that is all references to parts of our main list. This will allow us to target deep inside of the array without knowing the depth, or all of the parents above the level. Let me explain how this will look in a more visual way. On the left, we have our references array and on the right we have the list we are building with multiple levels. Now you have two ways to make an adjustment to a variable deep inside the main array, such as adding ‘Miami’ to Floria.

$main_list[‘North America’][‘children’][‘United States’][‘children’][‘Florida][‘children’][] = ‘Miami’; or $references[‘Florida’][‘children’][] = ‘Miami’; You can see how it is much easier to target deep inside of the array using the reference, rather having to know the entire structure above where Florida is.

ALL TOGETHER NOW

So when you extract the data from the database you will do the following process, repeating until all the data has been processed.

  1. Add item to reference list
  2. If item has no parent (meaning it’s a top level item), add item reference to top level of build list.
  3. If item has a parent, add the item reference to it’s parents list of children.

WATCH IT STEP BY STEP

If I’ve completely lost you, I suggest that you take a look how this works step by step. I’ve provided an example of building the location list above, where it prints out what the reference and main list look like after each item as been added. You can view that here.

发表评论