PHP Recursive infinite Pole Classification example

Source: Internet
Author: User

Introduction to the principle of infinite class classification

The infinite classification seemingly "tall", in fact, the principle is very simple. Infinite classification needs not only the subtlety of code, but also the rationality of database design. To satisfy an infinite class of classifications, the database needs to have two required fields, Id,pid. The ID is used to identify itself, and the PID is used to indicate the parent ID. That is, each classification record not only describes itself, but also describes another ID that is most closely related to it. Seemingly complex things are solved by such a small skill.

Gossip doesn't say much, it's time to show examples of this article.

As a fan of crazy pirates, this example I will be "The King of Thieves" character organization to do cases.

  Database preparation: 

Build Table OnePiece:

Table onepiece (    int auto_increment,    nullvarchar (nullkey(ID));  

Insert test data:

Insert OnePieceValues(1,0,‘Navy‘), (2,0,‘Sea thief‘), (3,0,‘Revolutionary‘), (4,1,‘Green Pheasant‘), (5,1,‘Red Dog‘), (6,1,‘Yellow Ape‘), (7,2,‘Four Kings‘), (8,2,‘Seven Wu Hai‘), (9,2,‘Straw hat and Sea Thief Regiment‘), (10,9,' Sauron '), (one,7,' The ' Fox '), (8,' Doverland ' ), ( 8,' Klockdal ');               

This is still a popular science in the King inside the set: The world divided into three camps: the Navy, pirates, the revolutionary Army. The Navy has generals: Green Pheasant, Red Dog, Yellow Ape. The Pirates are: Four kings, seven Wu Hai, Straw Hat Pirates regiment. The four Kings have the incense, seven Wu Hai has many flamenco elder brother, the Klockdal, The Straw Hat Thief Regiment has Sauron. (Make an advertisement: The thief Wang Zhen is very good).

  Ultimate Purpose:

Today we are making two forms of infinite classification, one is a drop-down list, and the other is navigation link. Directly on the:

Drop-down list navigation link

 Instance code:

I encapsulated a unlimited class that calls Diaplaylist () to show the drop-down list form, calling Diaplaylink to show the navigation link category. You can also add (AddNodes ()) and delete (Deletenodes) classifications.

  

<?PhpClassunlimited{Protected$mysqli;Publicfunction __construct ($config){$this->mysqli=New Mysqli ($config [' Host '],$config [' User '],$config [' pwd ']);$this->mysqli->select_db ($config [' DB ']);$this->mysqli->set_charset (' UTF8 ');if ($this->mysqli->Connect_errno) {Echo$this->mysqli->Connect_error; } }Privatefunction GetList ($pid =0,&$result =Array (),$spac =0){$spac =$spac +2;$sql = "SELECT * from OnePiece where pid={$pid} ";$rs =$this->mysqli->query ($sql);While$row =$rsFETCH_ASSOC ()) {$row [' Name ']=Str_repeat (' &nbsp;&nbsp ',$SPAC).$row [' Name '];$result []=$row;$this->getlist ($row [' id '],$result,$spac); }Return$result; }/** Show drop-down list category * @return [type]*/PublicfunctionDisplaylist () {$rs =$thisGetList ();$str = "<select name= ' Cate ' >";foreach ($rsAs$key =$val) {$str. = "<option >{$val [' Name ']}</option> '; }$str. = "</select>";Return$str; }Privatefunction GetLink ($cid,&$result =Array()){$sql = "SELECT * from OnePiece where id={$CID} ";$rs =$this->mysqli->query ($sql);If$row =$rsFETCH_ASSOC ()) {$result []=$row;$this->getlink ($row [' pid '],$result); }ReturnArray_reverse ($result); }/** * Show Navigation link * @param [type] $cid [description] * @return [type] [description]*/Publicfunction DisplayLink ($cid){$rs =$this->getlink ($cid);$str = ";foreach ($rsAs$val) {$str. = "<a href=" >{$val [' Name ']}</a>> '; }Return$str; }/** * Added category * @param [Type] $PID parent class ID * @param [type] $name this class name*/Publicfunction AddNodes ($pid,$name){$sql = "INSERT into onepiece values (', {$pid}, ' ".$name. "')";If$this->mysqli->query ($sql)){ReturnTrue; } }/** * Delete category * @param [Type] $id this class ID * @return [type]*/Publicfunction Deletenodes ($id){$sql = "SELECT * from OnePiece where PID ={$ID} ";$rs =$this->mysqli->query ( $sql ); if ( $row = $rs->< Span style= "color: #000000;" >FETCH_ASSOC ()) { $mes = "There are also child elements, do not delete the" ;} else{ $sql = "Delete From OnePiece where Id={ $id} "if ( $this->mysqli->query (  $sql )) { $mes = "Delete succeeded" return  $mes    

PHP recursive infinite-pole classification instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.