PHP development classification technology drop-down menu classification (2)

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);
?>
Continuing Learning
||
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
header("content-type:text/html;charset=utf8");
$link = mysqli_connect('localhost','username','password','test');
mysqli_set_charset($link, "utf8");
if (!$link) {
die(":".mysqli_connect_error());
}
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);
?>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
submitReset Code
图片放大关闭