PHP infinite classification drop-down list classification (Part 1)

Implementation schematic

递归.png

# #Drop-down list infinite level classification

<?php
include ("conn.php");
function getList($pid=0,&$result=array(),$space=0){
    $space=$space+2;
    $sql="SELECT*FROM deepcate WHERE pid = $pid";
    $res = mysql_query($sql);
    while ($row = mysql_fetch_assoc($res)){
        $row['catename']=str_repeat('&nbsp;',$space).'|--|'.$row['catename'];
        $result[]=$row;
        getList($row['id'],$result,$space);
    }
    return $result;
}
$rs=getList();
function displayCate($pid=0,$selected=1){
    $rs=getList($pid);
    $str='';
    $str.="<select name='cate'>";
    foreach ($rs as $key=>$val){
        $selectedstr='';
        if ($val['id'] == $selected){
            $selectedstr="selected";
        }
        $str.="<option{$selectedstr}>{$val['catename']}</option>";
    }
    return $str.='</select>';
}
echo displayCate(0,2);
?>

The example is an infinite level classification. Below we will explain the code in sections.

Code explanation:

<?php
include ("conn.php");
function getList($pid=0,&$result=array(),$space=0){
   $space=$space+2;
   $sql="SELECT*FROM deepcate WHERE pid = $pid";
   $res = mysql_query($sql);
   while ($row = mysql_fetch_assoc($res)){
       $row['catename']=str_repeat('&nbsp;',$space).'|--|'.$row['catename'];
       $result[]=$row;
       getList($row['id'],$result,$space);
   }
   return $result;
}
$rs=getList();
?>
function getList($pid=0){
}
?>

First define a function, with pid as the parent class ID, defined as 0.

$sql="SELECT*FROM deepcate WHERE pid = $pid";

Use sql statement to query the subclass of pid selection surface.

$res = mysql_query($sql);
//执行sql语句
    $result=array();
   while ($row = mysql_fetch_assoc($res)){
       $result[]=$row;
      }
   return $result;

Put the result into the array, and then return it to the result.

Recursion is the technique for the function to call itself. When querying the subclass, we need to call getList($row['id']);


The ID of the subclass must be As the next-level ID, we need to bring in $row['id'])

The code at this time is

<?php
function getList($pid=0){
$sql="SELECT*FROM deepcate WHERE pid = $pid";
$res = mysql_query($sql);
  $result=array();
   while ($row = mysql_fetch_assoc($res)){       $result[]=$row;      }   return $result;
}
?>

Recursive operations need to return an array, we use references Method, make changes to the code


<?php
include ("conn.php");
function getList($pid=0,&$result=array()){
   $sql="SELECT*FROM deepcate WHERE pid = $pid";
   $res = mysql_query($sql);
   while ($row = mysql_fetch_assoc($res)){
       $result[]=$row;
       getList($row['id'],$result);
   }
   return $result;
}
?>

Next, beautify the classified style

<?php
include ("conn.php");
function getList($pid=0,&$result=array(),$space=0){
   $space=$space+2;
   $sql="SELECT*FROM deepcate WHERE pid = $pid";
   $res = mysql_query($sql);
   while ($row = mysql_fetch_assoc($res)){
       $row['catename']=str_repeat('&nbsp;',$space).'|--|'.$row['catename'];
       $result[]=$row;
       getList($row['id'],$result,$space);
   }
   return $result;
}
$rs=getList();
print_r($rs);
?>

Print it out for observation.


Key points of this chapter

  1. Recursion to achieve infinite classification is to use recursion to find the parent node and generate a family tree.

  2. Subclasses and parent classes.

Continuing Learning
||
<?php include ("conn.php"); function getList($pid=0,&$result=array(),$space=0){ $space=$space+2; $sql="SELECT*FROM deepcate WHERE pid = $pid"; $res = mysql_query($sql); while ($row = mysql_fetch_assoc($res)){ $row['catename']=str_repeat(' ',$space).'|--|'.$row['catename']; $result[]=$row; getList($row['id'],$result,$space); } return $result; } $rs=getList(); print_r($rs); ?>
submitReset Code