We first define a function getList, parent class pid=0;
Use SQL statements to query subclasses.
Put the queried subclasses out through a while loop, and then add the "| -- " style,
Recursion is the technique of the function itself calling itself. When querying subclasses, we need to call getList($row['id']);
The ID of the subclass should be used as the ID of the next level, so $row['id']) needs to be brought in later
Then print_r to print the output style.
<?php function getList($pid=0,&$result=array(),$space=0){ global $link; $space=$space+2; $sql="select * from class where pid = $pid"; $res = mysqli_query($link,$sql); while ($row = mysqli_fetch_assoc($res)){ $row['title']=str_repeat(' ',$space).'|-- '.$row['title']; $result[]=$row; getList($row['id'],$result,$space); } return $result; } $rs=getList(); print_r($rs); ?>Next Section