php+mysql Infinite class instance without recursive implementation of non recursive
- 2021-07-07 06:48:48
- OfStack
Recursion 1 is generally the first and easiest to think of to achieve infinite classification, but recursion 1 is generally considered as a resource-consuming method, so many systems do not consider using recursion
In this paper, the database is designed and implemented with an sql statement
The database fields are roughly as follows:
id Numbering
fid Parent classification number
class_name Classification name
path Classification path to id Is a node with a similar composition ,1,2,3,4, Such a string
The following data can be assumed:
id fid class_name path
1 0 Classification 1 , 1,
2 0 Classification 2 , 2,
3 1 Classification 1-1 , 1,3,
4 1 Classification 1-2 , 1,4,
5 2 Classification 2-1 , 2,5,
6 4 Classification 1-2-1 , 1,4,6,
Unlimited classification operation 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>';
}
?>
$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)) {
/**
* No. 1 1 Species display method
*/
/*$space = str_repeat ( ' ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
echo $space . $row ['name'] . '
';*/
/**
No. 1 2 Species display method
*/
$space = str_repeat ( ' - ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
$option .= '' . $space . $row ['name'] . '<Br>';
}
echo $option;
exit();
echo '<select name="opt">' . $option . '</select>';
Among them, $nbs is a database operation class, which is simple and clear!