PHP Infinitus classification method

墨辰丷
Release: 2023-03-29 21:18:02
Original
1450 people have browsed it

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 &#39;UTF-8&#39;");
$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[&#39;brand_id&#39;],$row[&#39;brand_name&#39;],$row[&#39;parent_id&#39;],$row[&#39;sort_order&#39;]);
}
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][&#39;2&#39;]==$id){
      if($class_arr[$i][&#39;0&#39;]==$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=&#39;0&#39;>顶级分类</option>
<?php
 echo $aa;
?>
</select>
Copy after login

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=&#39;0&#39;>顶级分类</option>
{$wuxian_class}
</select>
Copy after login

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][&#39;2&#39;]==$id){
      if($arr[$i][&#39;0&#39;]==$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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template