Phpmysql implements unlimited classification | tree display of classification relationships

Source: Internet
Author: User
Unlimited classification is achieved by storing the id and path of the upper-level classification. Because the data structure is simple, we need to display the classification relationship in a tree. I can only use the progressive method for implementation. below is the structure and

Unlimited classification is achieved by storing the id and path of the upper-level classification. Due to the simple structure of the data, to display the classification relationship in a tree, I can only use the progressive method for implementation. Below is a tree-like display function written by myself in the structure of the classification data table, I hope you can point out anything wrong.

Table structure: the id field is the category id, the name field is the category name, the father_id field is the id of the parent category, and the path field is the category path (store the clustering of the category's ancestors ), isdir determines whether a directory is used (1 is yes, 0 is no ).

Display function:

// $ Count indicates the classification level
Sort_list ($ str, $ fatherid, $ count)
{
$ Rs = $ this-> SQL-> re_datas ('select * from sort where father_id = fatherid ');
$ Num = $ this-> SQL-> SQL _numrows ();
$ I = 0;
$ N = 1;
While (isset ($ rs [$ I])
{
$ Name = '';
For ($ n = 1; $ n <$ count; $ n)
{
$ Name. = '│ ';
}
If ($ I 1 = $ num)
{
$ Name. = 'Your-'. $ rs [$ I] [name];
}
Else
{
$ Name. = 'Your-'. $ rs [$ I] [name];
}
If ($ rs [$ I] [isdir])
{
$ Str. = ''. $ name .'';
}
Else
{
$ Str. = $ name ';
}
$ Temp = $ count 1;
$ Str = $ this-> sort_list ($ str, $ rs [$ I] [id], $ temp );
$ I;
}
Return $ str;
}


$ This-> The SQL object is the SQL control class object. the re_datas () function returns the queried array and the SQL _numrows () function returns the queried number.
 
Call method: $ sort_list = sort_list ($ sort_list, 0, 1 );

The above are my personal thoughts. I also hope you can provide more guidance. By 5 W desk lamp

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.