PHP through the forward traversal tree to achieve the need for recursive infinite pole classification _php skills

Source: Internet
Author: User
Tags php programming

In this paper, we describe the infinite polar classification of PHP through the forward traversal tree without recursion. Share to everyone for your reference. Specifically as follows:

We usually use recursion to realize the infinite polar classification all know recursion efficiency is very low, the following introduces an improved first order traversal tree algorithm, does not apply to the recursive implementation of the infinite Polar classification, in the large amount of data to achieve a tree-level structure of the higher efficiency.

The SQL code is as follows:

CREATE TABLE IF not EXISTS ' category ' (
 ' id ' int (one) not null auto_increment,
 ' title ' varchar NOT NULL,
 ' L  FT ' int (one) not null,
 ' RGT ' int (one) not NULL,
 ' order ' int (one) not null COMMENT ' sort ',
 ' create_time ' int (one) not NULL,
 PRIMARY KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8 auto_increment=12;
---The
data in the Dump table ' category '-
-
INSERT into ' category ' (' id ', ' title ', ' LfT ', ' RGT ', ' order ', ' create_time ') V Alues
(1, ' Top columns ', 1, 1, 1261964806),
(2, ' edited categories ', ",", "1264586212"),
(4, ' Company Products ', 10, 15, 50, 126 4586249),
(5, ' honors ', 8, 9, 1264586270),
(6, ' data downloads ', 6, 7, X, 1264586295),
(7, ' talent recruitment ', 4, 5, 50, 12645 86314),
(8, ' Message board ', 2, 3, 1264586884),
(9, ' President ', M, X, 1267771951),
(10, ' subcategory of the new classification ', 11, 14, 0, 14 00044841),
(one, ' php dot-dot pass-http://www.phpddt.com ', 12, 13, 0, 1400044901);

The PHP code is as follows:

<?php/** * Pure Test * * @author Mckee * @link http://www.phpddt.com/class Category extends Ci_controller {pub
    Lic function __construct () {parent::__construct ();
  $this->load->database (); Public Function view () {$lists = $this->db->order_by (' LfT ', ' ASC ')->get (' category ')->result_array ()
    ; The right value of the first of the adjacent two records is larger than the second one. So that's his parent class//We use an array to store the right value of the previous record. Compare it with the right value of this record, if the former is smaller than the latter, it is not a parent-child relationship, use Array_pop to eject the array, otherwise keep/
    Two loops, no recursive $parent = Array ();
    $arr _list = Array (); foreach ($lists as $item) {if (count ($parent)) {while (count ($parent)-1 > 0 && $parent [count ($pare
        NT) -1][' RGT '] < $item [' RGT ']) {array_pop ($parent);
      $item [' depath '] = count ($parent);
      $parent [] = $item;
    $arr _list[]= $item; }//Show tree-like foreach ($arr _list as $a) {echo str_repeat ('--', $a [' Depath ']). $a [' title '].
    ' <br/> '; }/** * * Insert operation is very simple to find its parent node, then the left value andThe right value is greater than the left value of the node of the parent node plus 2, and then insert this node, respectively, the left value of the parent node plus one and plus two */Public function Add () {//Get the ID of the parent category $parent _id = 10;
    $parent _category = $this->db->where (' id ', $parent _id)->get (' category ')->row_array (); 1. The left and right values are greater than the left values of the nodes of the parent node plus 2 $this->db->set (' lft ', ' LFT + 2 ', FALSE)->where (Array (' LfT > ' => $parent _cate
    gory[' LfT '])->update (' category '); $this->db->set (' RGT ', ' RGT + 2 ', FALSE)->where (Array (' RGT > ' => $parent _category[' lft '))->update ('
    Category '); 2. Insert new node $this->db->insert (' category ', Array (' title ' => ' subcategory of new classification ', ' LFT ' => $parent _catego
    ry[' LFT '] + 1, ' RGT ' => $parent _category[' lft '] + 2, ' order ' => 0, ' Create_time ' => time ()
    ));
  Echo ' Add success ';
   /** * Delete *//1. The deleted node, the right value minus the left value and then add 1, get the value $width = $rgt-$lft + 1;
   *//2. Deletes all nodes between the left and right values *//3. Modify all nodes that are greater than the correct value of this node by subtracting their left or minus $width/public Function Delete () {//) to get the classification by category ID $id = 3;
    $category = $this->db->where (' id ', $id)->get (' category ')->row_array ();
    Compute $width $width = $category [' RGT ']-$category [' lft '] + 1;
    1. Delete the category $this->db->delete (' category ', array (' ID ' => $id)); 2. Delete all categories between the left and right values $this->db->delete (' category ', Array (' LfT > ' => $category [' LfT '], ' LfT < ' => $catego
    ry[' RGT ')); 3. Modify the value of other nodes $this->db->set (' lft ', ' LfT-{$width} ', FALSE)->where (Array (' LfT > ' => $category [' RGT '])-
    >update (' category '); $this->db->set (' RGT ', ' RGT-{$width} ', FALSE)->where (Array (' RGT > ' => $category [' RGT '])->update ('
    Category ');
  Echo ' Delete Success ';
    }//Edit, Public function edit () {//Needless to say, directly via ID edit $id = 2;
    $this->db->update (' category ', Array (' title ' => ' edited category '), array (' ID ' => $id));
  Echo ' edit success ';

 }
}

I hope this article will help you with your PHP programming.

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.