Tree-type Display category

Source: Internet
Author: User
Tree-type Display classification
This post was last edited by bing15 on 2013-02-02 14:07:19

Get the data from the database and display the classification in tree form, such as:
Notice
Article
-New articles
-Hot Articles

The structure of the taxonomy database is:
Id,reid,topid,title

ID: Category ID
Reid: Parent class ID
TOPID: Top ID
Title: Name
Let me give you a little bit of data, which is easier to do.
ID Reid topid Title
1 0 0 Announcements
2 0 0 Articles
3 2 2 new articles
4 2 2 hot Articles

Remember: There may be a small category in the new article, Oh, Infinite class classification.


------Solution--------------------
 $arr = Array (

Array (' ID ' =>1, ' city_name ' = ' China ', ' rel_id ' = ' 1 ', ' pid ' =>0),
Array (' ID ' =>2, ' city_name ' = ' Guangdong ', ' rel_id ' = ' 1-2 ', ' pid ' =>1),
The array (' ID ' =>3, ' city_name ' = ' Shenzhen ', ' rel_id ' and ' I ', ' pid ' =>2),
Array (' ID ' =>4, ' city_name ' = ' guangzhou ', ' rel_id ' = ' 1-2-4 ', ' pid ' =>2)

);

function Find_subclass ($pid) {

Global $arr;
$__arr = Array ();
foreach ($arr as $k = $v)
{

if ($v [' pid ']== $pid) $__arr[] = $v;

}
return $__arr;

}

function Tree_subclass ($pid =0) {

$__arr = Array ();
$__arr = Find_subclass ($pid);
if (!empty ($__arr)) {

foreach ($__arr as $k = $v)
{

$__arr[$k] [' subclass '] = Tree_subclass ($v [' id ']);

}

}
return $__arr;
}

Var_dump (Tree_subclass (0));

?>

------Solution--------------------
Reference:
How does my data recursively display sub-categories under classification?
See http://bbs.csdn.net/topics/390364669
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.