Php+mysql realize Infinite class classification | Tree-type display classification relationship _php skills

Source: Internet
Author: User
Infinite level classification, mainly by storing the ID of the superior category and the classification path to achieve. Because of the simple structure of the data, I can only think of a recursive way to make the relationship of the classification a tree-like display.


Infinite level classification, mainly by storing the ID of the superior category and the classification path to achieve. Because the structure of the data is simple, so the relationship between the classification by the tree, I can only think of a recursive way to achieve, the following is a classification of data table structure and write a tree-like display function, there is nothing wrong where I hope you can point out.
Table structure: The ID field is the category identifier, the Name field is the category name, the father_id field is the Id,path field for the parent category (the collection that stores the ancestor of the taxonomy), and Isdir determines whether it is a directory (1 is, 0 is no).

Display function:

Copy Code code as follows:
$count for 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. = "└─". $rs [$i][name];
}
Else
{
$name. = "├─". $rs [$i][name];
}
if ($rs [$i][isdir])
{
$str. = "<span style= ' color: #CCCCCC ' >". $name. " </span> ";
}
Else
{
$str. = $name ";
}
$temp = $count +1;
$str = $this->sort_list ($str, $rs [$i][id], $temp);
$i + +;
}
return $str;
}


Where the $this->sql object is the SQL Action class object, the Re_datas () function returns the array found, and the Sql_numrows () function returns the number of queries

Call method: $sort _list = sort_list ($sort _list,0,1);

The above is a personal thought, but also hope that everyone a lot of guidance
Related Article

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.