This article describes the method of realizing the infinite pole classification of smarty template in PHP. Share to everyone for your reference, specific 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 ' > top class </option> <?php
echo $aa;?> </select>
If it's in Smarty,
Add one more.
Copy Code code as follows:
$smarty->assign (' Wuxian_class ', $AA);
Just add this to the HTML page.
<select name= "parent_id" >
<option value= ' 0 ' > top class </option>
{$wuxian _class}
</ Select>
There is also a problem to note $str must be global line, otherwise there will be errors.
Extension of the infinite polar classification:
We can write a function, each time we call this function we just need to pass in the array and data, we can call, the following 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;
}
The call only needs to do this:
Copy Code code as follows:
$AA = Dafenglei_select ($class _arr,0,0,0);
I hope this article will help you with the PHP program design based on Smarty template.