The principle of infinite classification in PHP

WBOY
Release: 2016-07-29 09:14:47
Original
1010 people have browsed it

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.

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

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

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

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.

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