Home > Backend Development > PHP Tutorial > Analysis of the specific principles of PHP infinite classification_PHP tutorial

Analysis of the specific principles of PHP infinite classification_PHP tutorial

WBOY
Release: 2016-07-15 13:30:29
Original
739 people have browsed it

For beginners, there are still many things in PHP that require in-depth exploration. Only through constant problem solving can we grasp the sincerity. What is PHP unlimited classification? Just like creating a new folder under Windows, you can create a new folder under the newly created folder. This will continue in an infinite loop. The same is true for infinite classification. A parent class can separate its subcategories, and a subcategory can separate its subclasses. class, and this continues in an infinite loop.

So how does PHP achieve its infinite classification? How to list its various categories one by one?
First of all, we assume that there is such a three-level classification, News→PHP News→PHP6.0 is out.
If we want to find the news "PHP6.0 is out", we can click on the news first, and then click on the PHP news to find it out. In other words, we can search down level by level through the grandfather category. , in turn, as long as we know the parent class of a subclass, we can find it. In this way, when designing the database, we can design an additional field of parent class id to realize the unlimited classification function of PHP.

//We create a table "class"
CREATE TABLE `class` (
`id` int(11) NOT NULL auto_increment COMMENT 'classification id',
`f_id` int (11) NOT NULL COMMENT 'parent id',
`name` varchar(25) collate gbk_bin NOT NULL COMMENT 'category name',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET =gbk COLLATE=gbk_bin AUTO_INCREMENT=1 ;

//First we insert the 'news' category into the database, because 'news' is the largest category and there is no parent category on it, so I put its f_id Set to 0.
INSERT INTO `class` (`id`, `f_id`, `name`) VALUES(1, 0, 'News'); //The id field grows automatically and no value is required.

//Then we insert the category 'PHP News' into the database. The id of its parent category 'News' is 1, so its f_id is set to 1.
INSERT INTO `class` (`id`, `f_id`, `name`) VALUES(2, 1, 'PHP News');

//Then we insert 'PHP6 into the database .0 came out' of this category, the id of its parent category 'PHP News' is 2, so its f_id is set to 2.
INSERT INTO `class` (`id`, `f_id`, `name`) VALUES(3, 2, 'PHP6.0 is out');

//Similarly, we can do this Inserting categories all the way down will reach unlimited categories in PHP.
//We can find that the key to inserting a category is to find the id of the parent category of this category, and then use it as the value of the f_id field of this category.
//Suppose you want to insert the category 'Technology' at the same level as 'News', that is to say, it is also the largest category and there is no parent category above, then its f_id is also set to 0;
INSERT INTO ` class` (`id`, `f_id`, `name`) VALUES(4, 0, 'Technology');

//There is another category 'PHP Technology' under 'Technology', then we How to insert it, first find the id of the parent class 'Technology' of 'PHP Technology', and then use it as the value of its own f_id field.
INSERT INTO `class` (`id`, `f_id`, `name`) VALUES(5, 4, 'PHP technology');

//After seeing this, everyone should understand How to insert each category into the database. No more examples.

We already know how to insert each category into the database, but how to list each category?

<ol class="dp-xml">
<li class="alt"><span><span class="tag">< ?</span><span class="tag-name">php</span><span> </span></span></li><li><span>header("Content-type:text/html;</span><span class="attribute">charset</span><span>=</span><span class="attribute-value">utf</span><span>-8");   </span></li><li class="alt"><span>$</span><span class="attribute">db</span><span>=</span><span class="attribute-value">new</span><span> mysqli("localhost","root","","news_php100") ; <br />//实例化一个数据库连接。使用这个前一定要确保已经加载了mysqli类库,<br />或者用mysql_connect这个方式连接。   </span></li><li><span>if(mysqli_connect_errno()){  </span></li><li class="alt"><span>echo "链接失败:".mysqli_connect_error();  </span></li><li><span>exit(); }   </span></li><li class="alt"><span>$db-</span><span class="tag">></span><span>query("set names utf8");  </span></span></li>
<li>
<span>$</span><span class="attribute">result</span><span>=$db-</span><span class="tag">></span><span>query("select name from class where </span><span class="attribute">f_id</span><span>=</span><span class="attribute-value">0</span><span>"); <br>//查找</span><span class="attribute">f_id</span><span>=</span><span class="attribute-value">0</span><span>的分类,也就是查找每一个大类。  </span>
</li>
<li class="alt">
<span>while($</span><span class="attribute">row</span><span>=$result-</span><span class="tag">></span><span>fetch_assoc()){  </span>
</li>
<li>
<span>echo $row['name']."</span><span class="tag">< </span><span class="tag-name">br</span><span class="tag">></span><span>"; //这样就把每个大类循环出来了。  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>//同样我们可以把新闻的子类循环出来。  </span></li>
<li class="alt">
<span>$</span><span class="attribute">result</span><span>=$db-</span><span class="tag">></span><span>query("select * from class where </span><span class="attribute">f_id</span><span>=</span><span class="attribute-value">1</span><span>"); <br>//查找</span><span class="attribute">f_id</span><span>=</span><span class="attribute-value">1</span><span>的分类,也就是查找‘新闻’的子类。  </span>
</li>
<li>
<span>while($</span><span class="attribute">row</span><span>=$result-</span><span class="tag">></span><span>fetch_assoc()){  </span>
</li>
<li class="alt"><span>echo $row['name']."  </span></li>
<li><span>"; //这样就把‘新闻’的子类循环出来了。注意:只是子类,不包括孙子类。  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>//写到这里,我们会发现一个问题,如果这个分类是10级分类,难道我们要写<br>10个循环把它每个子类循环出来?如果是更多级分类呢,这样写显然是不现实的。  </span></li>
<li class="alt"><span>//那又有什么办法解决呢?我们可以写一个递归的函数,把f_id作为参数传入,<br><br>不断循环每一个f_id的值,也就是说把每一个f_id值的子类循环出来。  </span></li>
<li><span>//首先我们把各个分类的值保存在一个二维数组中,在下面的递归函数里有用。  </span></li>
<li class="alt">
<span>$</span><span class="attribute">result</span><span>=$db-</span><span class="tag">></span><span>query("select * from class");  </span>
</li>
<li>
<span>while($</span><span class="attribute">row</span><span>=$result-</span><span class="tag">></span><span>fetch_assoc()){  </span>
</li>
<li class="alt"><span>$arr[]=array($row[id],$row[f_id],$row[name]); //每一行保存一个<br>分类的id,f_id,name的信息。  </span></li>
<li><span>}  </span></li>
<li class="alt">
<span>function fenlei($</span><span class="attribute">f_id</span><span>=</span><span class="attribute-value">0</span><span>){ //$f_id初始化为0,也就是从最大分类开始循环.  </span>
</li>
<li><span>global $arr; //声明$arr为全局变量才可在函数里引用。  </span></li>
<li class="alt">
<span>for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag">< </span><span class="tag-name">count</span><span>($arr);$i++){ //对每个分类进行循环。  </span>
</li>
<li>
<span>if($arr[$i][1]==$f_id){ //$arr[$i][1]表示第$i+1个分类的f_id的值。<br>开始$</span><span class="attribute">f_id</span><span>=</span><span class="attribute-value">0</span><span>,也就是把</span><span class="attribute">f_id</span><span>=</span><span class="attribute-value">0</span><span>的分类输出来。  </span>
</li>
<li class="alt">
<span>echo $arr[$i][2]."</span><span class="tag">< </span><span class="tag-name">br</span><span class="tag">></span><span>"; //$arr[$i][1]表示第$i+1个分类的name的值。  </span>
</li>
<li><span>fenlei($arr[$i][0]); //$arr[$i][1]表示第$i+1个分类的id的值。进行递归<br>,也就是把自己的id作为f_id参数把自己的子类再循环出来。  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>fenlei(); //使用这个函数.  </span></li>
<li class="alt">
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copy after login

The above code example explains in detail the principles and usage of PHP unlimited classification. I hope it will be helpful to everyone.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446274.htmlTechArticleFor beginners, there are still many things in PHP that require in-depth exploration. Only through constant problem solving can we grasp the sincerity. What is PHP unlimited classification? Just like window...
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