PHP+mysql implements infinite classification | tree display classification relationship_PHP tutorial

WBOY
Release: 2016-07-21 16:00:16
Original
901 people have browsed it

Unlimited classification is mainly achieved by storing the id and classification path of the upper-level classification. Since the structure of the data is simple, to display the classification relationship in a tree, I can only think of using recursion to implement it.


Unlimited classification is mainly achieved by storing the id and classification path of the upper-level classification. Since the structure of the data is simple, I want to display the classification relationship in a tree shape. I can only think of using recursion to implement it. The following is the classification data table structure and a tree display function written by myself. What is wrong? I hope everyone can point it out.
Table structure: The id field is the category identifier, the name field is the category name, the father_id field is the id of the parent category, the path field is the category path (storing the collection of ancestors of the category), isdir determines whether it is a directory (1 means yes , 0 means no).

Display function:

Copy code The code is as follows:
//$count is 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.="└─".$rs[$i][name];
}
else
{
$name.="├─".$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;
}


Where $this->sql object is the sql operation class object, and the re_datas() function returns the found array, The sql_numrows() function returns the queried number

Calling method: $sort_list = sort_list($sort_list,0,1);

The above are my personal thoughts, and I hope everyone can give me some advice

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317118.htmlTechArticleUnlimited classification is mainly achieved by storing the id and classification path of the upper-level classification. Since the structure of the data is simple, to display the classification relationship in a tree shape, I can only think of using recursion...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template