An implementation method of infinite classification in PHP _php instance

Source: Internet
Author: User

Unlimited class classification is a design technique that is often used in development, such as: Site Directory, departmental structure, article classification. I think it plays a great role in the hierarchy of the design table, for example, in some platforms,

Fill in the invitation, it is a kind of subordinate relationship, the superior will have a number of subordinates, subordinates will have their own branches, most of the use of the idea of recursion to achieve. Don't say much, first to warm up the realization of recursion

Recursion (programming techniques that the program calls itself):

1, $_globals[result]

2. Static $result

3. Parameter Reference &

For example: Traverse 1-10

"'
$i =0;
function Deeploop ($i) {
  global $i;
  $i + +;
  echo $i;
  if ($i <10) {
    deeploop ($i);
  }

}

function Deeploop () {
  static $i =0;
  $i + +;
  echo $i;
  if ($i <10) {
    deeploop ($i);
  }

}


Function Deeploop (& $i =0) {
  $i + +;
  echo $i;
  if ($i <10) {
    deeploop ($i);
  }

}

```

One, infinite classification implementation:

1, table design settings parent ID top-level parent ID set to 0 to establish a tree; each category needs to record its parent ID. (Pid=0 represents the top level)

ID pid catename cateorder createtime (primary key ID, parent ID, category name, sorting sort, creation time)

Example: The classification structure of the website; the structure of food and beverage;

2, the full path of the infinite classification (in a field of all the parent ID is recorded sequentially)

ID path catename cateorder createtime (primary key ID, comma delimited order of parent ID, category name, sort order, creation time)

Disadvantages

Full-path query convenience, increase, mobile classification of data maintenance slightly more complex;

Second, the implementation of examples (site directory):

Classification table:

  ' #父级Id递归法 CREATE TABLE ' deepcate ' (' id ' int (one) not NULL auto_increment PRIMARY KEY, ' pid ' int (1 1) NOT null DEFAULT 0, ' catename ' char (= NOT null, ' Cateorder ' int (6), ' createtime ' date) engine=
    
    
    INNODB default Charset=utf8;
                  Data INSERT into ' deepcate ' VALUES (1, 0, ' picture ', null, ' 2016-11-01 '), (2, 1, ' Belle ', NULL, ' 2016-11-01 '), (3, 1, ' News ', NULL, ' 2016-11-01 '), (4, 2, ' soccer baby ', null, ' 2016-11-01 '), (5, 2, ' Japan, Korea
                  
    Star ', NULL, ' 2016-11-01 '), (6, 5, ' beauty photo ', null, ' 2016-11-01 '); #全路径 CREATE TABLE ' qljcate ' (' id ' int (one) not NULL, ' path ' char (255), ' Catename ' char (?) NOT NULL
    
    , ' Cateorder ' int (6), ' createtime ' date ' engine=innodb default Charset=utf8;
              INSERT into ' qljcate ' VALUES (1,null, ' picture ', null, ' 2016-11-01 '), (2, 1, ' beauty picture ', null, ' 2016-11-01 '), (3,1,2, ' football baby ', NULL,' 2016-11-01 '), (4,1,2, ' Japanese and Korean stars ', null, ' 2016-11-01 '), (5,1,2,4, ' beauty photo ', null, ' 2016-11-01 '); ```

    Get the directory path where the picture is located:
  

  ' #父级Id递归法 CREATE TABLE ' deepcate ' (' id ' int (one) not NULL auto_increment PRIMARY KEY, ' pid ' int (1 1) NOT null DEFAULT 0, ' catename ' char (= NOT null, ' Cateorder ' int (6), ' createtime ' date) engine=
    
    
    INNODB default Charset=utf8;
                  Data INSERT into ' deepcate ' VALUES (1, 0, ' picture ', null, ' 2016-11-01 '), (2, 1, ' Belle ', NULL, ' 2016-11-01 '), (3, 1, ' News ', NULL, ' 2016-11-01 '), (4, 2, ' soccer baby ', null, ' 2016-11-01 '), (5, 2, ' Japan, Korea
                  
    Star ', NULL, ' 2016-11-01 '), (6, 5, ' beauty photo ', null, ' 2016-11-01 '); #全路径 CREATE TABLE ' qljcate ' (' id ' int (one) not NULL, ' path ' char (255), ' Catename ' char (?) NOT NULL
    
    , ' Cateorder ' int (6), ' createtime ' date ' engine=innodb default Charset=utf8;
              INSERT into ' qljcate ' VALUES (1,null, ' picture ', null, ' 2016-11-01 '), (2, 1, ' beauty picture ', null, ' 2016-11-01 '), (3,1,2, ' football baby ', NULL,' 2016-11-01 '), (4,1,2, ' Japanese and Korean stars ', null, ' 2016-11-01 '), (5,1,2,4, ' beauty photo ', null, ' 2016-11-01 '); ```

* Note points:

The move category cannot be moved below itself and its subclasses; Delete categories (only the lowest-level categories can be deleted, and classes without subclasses; in other words, they can only be removed from the bottom).

Think about all the picture categories under the picture type?

'
  #pid Parent ID Implementation
  function getallcate ($id,& $result =array ()) {
    $sql = "SELECT * from Deepcate WHERE pid ({$id}) ";
    $query = mysql_query ($sql);
    $row = Mysql_fetch_assoc ($query);
    if (mysql_num_rows ($row) >0)
    {
      $idlist = array ();
      while ($row) {
        $result [] = $row;
        $idlist [] = $row [' id ']; 
      }
      $id = Implode (', ', $idlist);
      Getallcate ($id, $result);
    
    }
    
    $result = Array_unique ($result);
    return $result;
  }
  ```

This kind of method is applicable to all books, articles under the parent category of the query ... Of course, the full path can be directly obtained, it is no longer proposed.

In practical operation, we can design reasonable table structure according to the actual situation.

For example, the chain stores to achieve the management of goods, store account settings will have a subordinate relationship, according to the division point of the partition may have their subordinates, then the use of this infinite classification method is more flexible to face this relationship system, just set each account level, You can assign permissions to different stores.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.