Home >Backend Development >PHP Tutorial >PHP Infinitus classification method
This article mainly introduces the PHP Infinitus classification method. Interested friends can refer to it. I hope it will be helpful to everyone.
The code is as follows:
<?php $conn = mysql_connect("localhost","admin","admin"); mysql_select_db("people_shop",$conn); mysql_query("SET NAMES 'UTF-8'"); $class_arr=array(); $sql = "select * from ecs_brand ORDER BY brand_id asc "; $query = mysql_query($sql); while($row = mysql_fetch_array($query)){ $class_arr[] = array($row['brand_id'],$row['brand_name'],$row['parent_id'],$row['sort_order']); } function dafenglei_select($m,$id,$index) { global $class_arr; global $str; $n = str_repeat(" ",$m); // $n = $m; for($i=0;$i<count($class_arr);$i++){ if($class_arr[$i]['2']==$id){ if($class_arr[$i]['0']==$index){ $str.= "<option value=\"".$class_arr[$i][0]."\" selected=\"selected\">".$n.$class_arr[$i][1]."</option>\n"; }else{ $str.= "<option value=\" ".$class_arr[$i][0]."\">".$n.$class_arr[$i][1]."</option>\n"; } dafenglei_select($m+1,$class_arr[$i][0],$index); } } return $str; } $aa = dafenglei_select(1,0,0); ?> <select name="parent_id"> <option value='0'>顶级分类</option> <?php echo $aa; ?> </select>
If you are using smarty
Add another
The code is as follows:
$smarty->assign('wuxian_class',$aa);
Just add this sentence to the html page
<select name="parent_id"> <option value='0'>顶级分类</option> {$wuxian_class} </select>
There is another question Note that $str must be a global line, otherwise there will be an error.
Extension of Infinitus classification:
We can write a function. Every time we call this function, we only need to pass in the array and data, and then we can call it Okay, here is a method I wrote.
function dafenglei_select(& $arr=array(),$m,$id,$index) { // global $class_arr; global $str; $n = str_repeat(" ",$m); // $n = $m; for($i=0;$i<count($arr);$i++){ if($arr[$i]['2']==$id){ if($arr[$i]['0']==$index){ $str.= "<option value=\"".$arr[$i][0]."\" selected=\"selected\">".$n.$arr[$i][1]."</option>\n"; }else{ $str.= "<option value=\" ".$arr[$i][0]."\">".$n.$arr[$i][1]."</option>\n"; } dafenglei_select($arr,$m+1,$arr[$i][0],$index); } } return $str; }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
How to implement WeChat’s custom sharing function based on thinkPHP
php version of WeChat red envelope interface usage example
php based on glob function to achieve detailed explanation of traversing files and directories
The above is the detailed content of PHP Infinitus classification method. For more information, please follow other related articles on the PHP Chinese website!