Table structure: The ID field is the category ID, the Name field is the category name, and the father_id field is the category path for the Id,path field of the parent category (Save the
Class ancestor, Isdir to determine whether it is a directory (1 is, 0 is no).
Example 1
| The code is as follows |
Copy Code |
$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);
Instance on 2
ID number
FID parent category number
Class_name category Name
Path classification paths, with IDs as nodes, composed of similar, 1,2,3,4, strings
———————————————————————————-
You can assume that the following data
ID FID class_name Path
—————————————————-
1 0 Categories 1, 1,
2 0 Categories 2, 2,
3 1 Categories 1-1, 1, 3,
4 1 Categories 1-2, 1, 4,
5 2 Categories 2-1, 2, 5,
6 4 Classification 1-2-1, 1,4,6,
—————————————————-
| The code is as follows |
Copy Code |
<?php $sql = "SELECT * from Tree order by path"; $result = $nbs->query ($sql); while ($rows = $nbs->fetch_array ($result)) { if (Substr_count ($rows [' Path '], ', ') >2) { For ($i =0 $i < (substr_count ($rows [' Path '], ', ')-2); $i + +) Echo '; } echo $rows [' class_name ']. <br> '; } ?> |
Code
| code is as follows |
copy code |
| $conn = mysql_connect (' localhost ', ' root ', ' root '); mysql_select_db (' wanggou123 ', $conn); mysql_query (' Set names UTF8 '); $sql = "Select Id,concat (Catpath, '-', id) as Abspath,name from category order by Abspath"; $query = mysql_query ($sql); while ($row =mysql_fetch_array ($query)) { /** * The first presentation method */ /* $space = str_repeat (' ', count ('-'), $row [' absp Ath '])-1); Echo $space. $row [' name ']. ' '; */ /** Second Presentation method */ $space = Str_repeat ('--', Count (Explode ('-', $row [' Abspath '])- 1); $option. = '. $space. $row [' name ']. ' <Br> '; } Echo $option; exit (); Echo ' <select name= opt > '. $option. ' </select> '; |