PHP development...LOGIN

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);
?>
Next Section
<?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); ?>
submitReset Code
ChapterCourseware