Home  >  Article  >  Backend Development  >  The principle of infinite classification in PHP

The principle of infinite classification in PHP

WBOY
WBOYOriginal
2016-07-29 09:14:47896browse

What is unlimited classification? Just like creating a new folder under Windows, you can create a new folder under the new folder, and this will continue endlessly. The same is true for infinite classification. The parent class can be divided into its subcategories, and the subcategories can be divided into it. subclass of , and this keeps looping endlessly . So how does PHP achieve its infinite classification? How to list its various categories one by one? First, 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 class, and vice versa. 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 achieve unlimited classification functions.

//我们建一个表"class"
CREATETABLE`class` (
  `id`int(11) NOTNULL auto_increment COMMENT '分类id',
  `f_id`int(11) NOTNULL COMMENT '父id',
  `name`varchar(25) collate gbk_bin NOTNULL COMMENT '分类名称',
  PRIMARYKEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=gbk COLLATE=gbk_bin AUTO_INCREMENT=1 ;//首先我们往数据库里插入‘新闻’这个大分类,因为‘新闻’是最大分类,上面没有父类了,所以我把它的f_id设置为0。
INSERTINTO`class` (`id`, `f_id`, `name`) VALUES(1, 0, '新闻');   //id这个字段是自动增长的,可以不写值。

//然后我们再往数据库里插入‘PHP新闻’这个分类,它的父类‘新闻’的id是1,所以它的f_id设置为1。
INSERTINTO`class` (`id`, `f_id`, `name`) VALUES(2, 1, 'PHP新闻');//然后我们再往数据库里插入‘PHP6.0出来了’这个分类,它的父类‘PHP新闻’的id是2,所以它的f_id设置为2。
INSERTINTO`class` (`id`, `f_id`, `name`) VALUES(3, 2, 'PHP6.0出来了');//同理,我们可以这样一直往下插入分类,也就达到了无限分类。
//我们可以发现插入一个分类的原则关键是找到这个分类的父类的id,然后作为这个分类的f_id字段的值。
//假设要插入跟‘新闻’同一个级别的分类‘技术’,也就是说它也是最大分类,上面没有父类了,那么它的f_id也设置为0;
INSERTINTO`class` (`id`, `f_id`, `name`) VALUES(4, 0, '技术');//在‘技术’下面又有一个分类‘PHP技术’,那么我们怎么插入呢,首先找到‘PHP技术’的父类‘技术’的id,然后作为自己的f_id字段的值。
INSERTINTO`class` (`id`, `f_id`, `name`) VALUES(5, 4, 'PHP技术');//看到这里,想必大家应该都明白怎么往数据库里插入各个分类了。就不再举例了。

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

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

http://blog.csdn.net/kao331431214/article/details/5425698

The above introduces the principle of unlimited classification in PHP, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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