PHP tree menu code based on recursive implementation, recursive php tree code
The example in this article describes the PHP tree menu code based on recursive implementation. Share it with everyone for your reference. The specific implementation method is as follows:
When developing an e-commerce website, I made this function to display a tree menu, using the PHP tree menu function implemented recursively. The specific code is as follows:
Copy code The code is as follows:
public function procCategory($sid,$pid){
$return = array();
$key = 0;
static $arr = array(); //Classification level reference array
$sql = "select cid,pcid,name from shop_goods_catalog where sid='{$sid}' and pcid = '{$pid}'";
$result = $this->__db->query($sql);
while($row=$this->__db->fetchArray($result)){
$nbsp = '';
if($row['pcid']==0){
$arr = array();
}
$arr[] = $row['pcid'];
//Top-level categories do not add tree structure identifiers.
if($row['pcid']>0){
//Add tree structure identification according to classification level
$key = array_search($row['pcid'],$arr);
for($i=0;$i<$key;$i++){
$nbsp .= ' ';
}
//Reconstruct the classification level reference array
if(count($arr)>1&&count(array_keys($arr,$row['pcid']))>1){
$arr = array_slice($arr,0,$key+1);
}
}
$row['name'] = $nbsp.$row['name'];
$row['level'] = $key; //Classification level, 0 is the top-level classification, 1 is the second-level classification, used for style setting or other needs
$return[] = $row;
$r = $this->procCategory($sid,$row['cid']);
$return = array_merge($return,$r);
}
return $return;
}
Since the efficiency of recursion is relatively low, if you focus on program efficiency, do not use this method, or improve this method.
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/914057.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914057.htmlTechArticlePHP tree menu code based on recursive implementation, recursive php tree code This article describes the php based on recursive implementation Tree menu code. Share it with everyone for your reference. Specific implementation method...