Home > Backend Development > PHP Tutorial > PHP uses recursive functions to achieve infinite classification

PHP uses recursive functions to achieve infinite classification

WBOY
Release: 2016-08-08 09:18:56
Original
1094 people have browsed it

I believe that many friends who are learning PHP will try to build an online shopping mall as a way to improve their skills. You should be comfortable with various operations on product classification, product names, etc., then you can try making unlimited classification lists.

What is infinite classification?

 Infinite-level classification is a classification technique, such as department organization, article classification, subject classification, etc. Infinite-level classification is commonly used. It can be simply understood as classification. In fact, if we think about it carefully, there are simply too many classifications in life. Clothes can be divided into men's clothing and women's clothing, tops and pants, and they can also be classified according to age groups. Classification is everywhere, and classification appears "infinite". I won’t talk about the necessity of infinite classification here.

Introduction to the Principle of Infinite Classification

 Infinite classification may seem "high-level", but in fact the principle is very simple. Infinite classification not only requires the ingenuity of the code, but also relies on the rationality of the database design. To satisfy infinite classification, the database needs to have two necessary fields, id and pid. The id is used to identify itself, while the pid is used to indicate the parent id. In other words, each classification record not only describes itself, but also describes another ID that it is most closely concerned about. What seemed like a complicated matter was solved with such a little trick.

  Without further ado, it’s time to show the examples of this article.

As an avid pirate fan, I will use the character organization of "One Piece" as an example in this article.

Database preparation:

Create table onepiece:

<span>create</span><span>table</span><span> onepiece(
    id </span><span>int</span><span> auto_increment,
    pid </span><span>int</span><span>not</span><span>null</span><span>,
    name </span><span>varchar</span>(<span>225</span>) <span>not</span><span>null</span><span>,
    </span><span>primary</span><span>key</span><span>(id)
);</span>
Copy after login

Insert test data:

<span>insert</span> onepiece <span>values</span><span>    (</span><span>1</span>,<span>0</span>,<span>'</span><span>海军</span><span>'</span><span>),
    (</span><span>2</span>,<span>0</span>,<span>'</span><span>海贼</span><span>'</span><span>),
    (</span><span>3</span>,<span>0</span>,<span>'</span><span>革命军</span><span>'</span><span>),
    (</span><span>4</span>,<span>1</span>,<span>'</span><span>青雉</span><span>'</span><span>),
    (</span><span>5</span>,<span>1</span>,<span>'</span><span>赤犬</span><span>'</span><span>),
    (</span><span>6</span>,<span>1</span>,<span>'</span><span>黄猿</span><span>'</span><span>),
    (</span><span>7</span>,<span>2</span>,<span>'</span><span>四皇</span><span>'</span><span>),
    (</span><span>8</span>,<span>2</span>,<span>'</span><span>七武海</span><span>'</span><span>),
    (</span><span>9</span>,<span>2</span>,<span>'</span><span>草帽海贼团</span><span>'</span><span>),
    (</span><span>10</span>,<span>9</span>,<span>'</span><span>索隆</span><span>'</span><span>),
    (</span><span>11</span>,<span>7</span>,<span>'</span><span>香克斯</span><span>'</span><span>),
    (</span><span>12</span>,<span>8</span>,<span>'</span><span>多弗朗明哥</span><span>'</span><span>),
    (</span><span>13</span>,<span>8</span>,<span>'</span><span>克洛克达尔</span><span>'</span>);
Copy after login

Here is the setting in One Piece under popular science: the world is divided into three camps: Navy, Pirates, and Revolution military. The navy has generals: Aokiji, Akainu, and Kizaru. The pirates include: the Four Emperors, the Shichibukai, and the Straw Hat Pirates. The Four Emperors have Shanks, the Shichibukai have Doflamingo and Crocodile, and the Straw Hat Pirates have Zoro. (Advertisement: One Piece is really good).

Ultimate goal:

What we are making today are two forms of infinite classification forms, one is a drop-down list type, and the other is a navigation link type. The renderings are shown directly:

Drop-down list styleNavigation Link style

Example code:

I encapsulated an Unlimited class to call diaplayList() to display the drop-down list form, and call diaplayLink to display the navigation Link category. You can also add (addNodes()) and delete (deleteNodes) categories.

<?<span>php

</span><span>class</span><span> Unlimited{
    </span><span>protected</span><span>$mysqli</span><span>;
    </span><span>public</span><span>function</span> __construct(<span>$config</span><span>){
        </span><span>$this</span>->mysqli=<span>new</span> mysqli(<span>$config</span>['host'],<span>$config</span>['user'],<span>$config</span>['pwd'<span>]);
        </span><span>$this</span>->mysqli->select_db(<span>$config</span>['db'<span>]);
        </span><span>$this</span>->mysqli->set_charset('utf8'<span>);
        </span><span>if</span> (<span>$this</span>->mysqli-><span>connect_errno) {
            </span><span>echo</span><span>$this</span>->mysqli-><span>connect_error;
        }
    }    

    </span><span>private</span><span>function</span> getList(<span>$pid</span>=0,&<span>$result</span>=<span>array</span>(),<span>$spac</span>=0<span>){
        </span><span>$spac</span>=<span>$spac</span>+2<span>;
        </span><span>$sql</span>="select * from onepiece where pid={<span>$pid</span>}"<span>;
        </span><span>$rs</span>=<span>$this</span>->mysqli->query(<span>$sql</span><span>);
        </span><span>while</span>(<span>$row</span>=<span>$rs</span>-><span>fetch_assoc()) {
            </span><span>$row</span>['name']=<span>str_repeat</span>(' &nbsp',<span>$spac</span>).<span>$row</span>['name'<span>];
            </span><span>$result</span>[]=<span>$row</span><span>;
            </span><span>$this</span>->getList(<span>$row</span>['id'],<span>$result</span>,<span>$spac</span><span>);            
        }
        </span><span>return</span><span>$result</span><span>;
    }
    </span><span>/*</span><span>*
     * 展现下拉列表式分类
     * @return [type] 
     </span><span>*/</span><span>public</span><span>function</span><span> displayList(){
        </span><span>$rs</span>=<span>$this</span>-><span>getList();
        </span><span>$str</span>="<select name='cate'>"<span>;

        </span><span>foreach</span> (<span>$rs</span><span>as</span><span>$key</span> => <span>$val</span><span>) {
            </span><span>$str</span>.="<option >{<span>$val</span>['name']}</option>"<span>;
        }
        </span><span>$str</span>.="</select>"<span>;
        </span><span>return</span><span>$str</span><span>;
    }

    </span><span>private</span><span>function</span> getLink(<span>$cid</span>,&<span>$result</span>=<span>array</span><span>()){
        </span><span>$sql</span>="select * from onepiece where id={<span>$cid</span>}"<span>;
        </span><span>$rs</span>=<span>$this</span>->mysqli->query(<span>$sql</span><span>);
        </span><span>if</span>(<span>$row</span>=<span>$rs</span>-><span>fetch_assoc()){
            </span><span>$result</span>[]=<span>$row</span><span>;
            </span><span>$this</span>->getLink(<span>$row</span>['pid'],<span>$result</span><span>);
        }
        </span><span>return</span><span>array_reverse</span>(<span>$result</span><span>);
    }
    </span><span>/*</span><span>*
     * 展现导航Link
     * @param  [type] $cid [description]
     * @return [type]      [description]
     </span><span>*/</span><span>public</span><span>function</span> displayLink(<span>$cid</span><span>){
        </span><span>$rs</span>=<span>$this</span>->getLink(<span>$cid</span><span>);
        </span><span>$str</span>=''<span>;
        </span><span>foreach</span> (<span>$rs</span><span>as</span><span>$val</span><span>) {
            </span><span>$str</span>.="<a href=''>{<span>$val</span>['name']}</a>>"<span>;
        }

        </span><span>return</span><span>$str</span><span>;
    }
    </span><span>/*</span><span>*
     * 增加分类
     * @param [type] $pid  父类id
     * @param [type] $name 本类名
     </span><span>*/</span><span>public</span><span>function</span> addNodes(<span>$pid</span>,<span>$name</span><span>){
        </span><span>$sql</span>="insert into onepiece values('',{<span>$pid</span>},'".<span>$name</span>."')"<span>;
        </span><span>if</span>(<span>$this</span>->mysqli->query(<span>$sql</span><span>)){

            </span><span>return</span><span>true</span><span>;

        }
    }
    </span><span>/*</span><span>*
     * 删除分类
     * @param  [type] $id 本类id
     * @return [type]     
     </span><span>*/</span><span>public</span><span>function</span> deleteNodes(<span>$id</span><span>){
        </span><span>$sql</span>="select * from onepiece where pid ={<span>$id</span>}"<span>;
        </span><span>$rs</span>=<span>$this</span>->mysqli->query(<span>$sql</span><span>);
        </span><span>if</span>(<span>$row</span>=<span>$rs</span>-><span>fetch_assoc()){
            </span><span>$mes</span>="还有子元素,请勿删除"<span>;
        }</span><span>else</span><span>{
            </span><span>$sql</span>="delete from onepiece where id={<span>$id</span>}"<span>;
            </span><span>if</span>(<span>$this</span>->mysqli->query(<span>$sql</span><span>)){
                </span><span>$mes</span>="删除成功"<span>;
            }
        }
        </span><span>return</span><span>$mes</span><span>;
    }
}</span>
Copy after login

 The functions in the class mainly adopt the method of recursive functions. If you understand the recursive functions deeply, the rest will fall into place. I will detail the three ways to implement recursive functions in later sections.

The above introduces the use of recursive functions in PHP to achieve infinite classification, 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