The principle of infinite classification: Just like creating a new folder under Windows, you can create a new folder under the newly created folder, and this will continue in an infinite loop. The same is true for infinite classification. The parent class can be divided into subclasses, and the subclasses can be divided into subclasses. Separate its subclasses and continue in an infinite loop
Example 1
The code is as follows | Copy code | ||||
1 => array('id'=>'1 ','parentid'=>0,'name'=>'First-level column one'), 2 => array('id'=>'2','parentid'=> 0,'name'=>'First-level column two'), 3 => array('id'=>'3','parentid'=>1,'name'=> 'Second-level column one'), 4 => array('id'=>'4','parentid'=>1,'name'=>'Second-level column two'), 5 => array('id'=>'5','parentid'=>2,'name'=>'Second-level column three'), 6 => array('id'=>'6','parentid'=>3,'name'=>'Third-level column one'), 7 => array('id'=> '7','parentid'=>3,'name'=>'Third-level column two'), 8 => array('id'=>'8','parentid'= >2,'name'=>'Second-level column three'), ); /** * Get the sub-ID of the current id * @param array $data original array * @param int $id current id * @param int $layer current level >*/ function genCate($data, $pid = 0, $level = 0) { if($level == 10) break; $l = str_repeat(" ", $ level); $l = $l.'└'; static $arrcat = array(); $arrcat = empty($level) ? array() : $arrcat; foreach ($ data as $ k = & gt; $ row) { /** * If the parent ID is the currently passed in id * / if ($ row ['paintid'] == $ pid ) 'level'] = $level; $arrcat[] = $row; ], $level+1);//Recursive call ); echo ""; |
Note: Because it is an infinite call, I added a judgment to let it jump out when the level $level=10. No normal website would have more than 10 levels of
directory structure.
After executing the static variable, determine the current level. If the level is 0, it means that this is the highest level menu. You need to clear the $arrcate data and re-declare it
Example 2
The code is as follows | Copy code | ||||||||
CREATE TABLE `class` ( `id` int(11) NOT NULL auto_increment COMMENT 'classification id',
|
The code is as follows | Copy code |
< ?php <🎜><🎜 >header("Content-type:text/html;charset=utf-8"); <🎜><🎜>$db=new mysqli("localhost","root","","news_php100") ; <🎜 >//Instantiate a database connection. Before using this, be sure to load the mysqli class library, <🎜> or use mysql_connect to connect. <🎜><🎜>if(mysqli_connect_errno()){<🎜><🎜>echo "Link failed:".mysqli_connect_error();<🎜><🎜>exit(); } <🎜><🎜>$db- >query("set names utf8");$result=$db->query("select name from class where f_id=0"); //Find the category of f_id=0 , that is, search for each major category. while($row=$result->fetch_assoc()){echo $row['name']."< br>"; //This way, each The major categories are cycled out. }//Similarly we can loop out the subcategories of news. $result=$db->query("select * from class where f_id=1"); //Find the category of f_id=1, that is, find the subcategory of 'news'. while($row=$result->fetch_assoc()){echo $row['name'].""; //This will The subcategory of 'News' is cycled out. Note: only subcategories, excluding grandchild categories. } |
//Writing here, we will find a problem. If this classification is a 10-level classification, do we have to write
10 loops to cycle out each of its subcategories? If there are more levels of classification, it is obviously unrealistic to write like this.
//Then what’s the solution? We can write a recursive function, passing in f_id as a parameter, and
continuously looping through each f_id value, that is to say, looping out the subclasses of each f_id value.
//First we save the values of each category in a two-dimensional array, which is useful in the following recursive function.
The code is as follows | Copy code | ||||
|
The code is as follows | Copy Code |
/** /** /** /** / ** /** /**
$select>$spacer$name" ("$nstr = "$str";"); $selected = $this->have($sid,$id) ? 'selected' : ' '; function have($list, $item){ |
Note: There is no platform restriction, you only need to tell the id, parentid, name
The three unlimited classification codes summarized above have no platform restrictions, but only It can be used in php. We only need to understand the relationship between id, parentid and name.
http://www.bkjia.com/PHPjc/444632.html