PHP implementation of infinite Class classification query (recursive, non-recursive), classification query recursive _php tutorial

Source: Internet
Author: User

PHP implements an infinite class of categorical queries (recursive, non-recursive), categorical query recursion


PHP for such a long time, found that the background management system is not a small application module is the classification of the column, in general, the columns are to be made unlimited, that is, each column theoretically can add sub-columns. In my opinion this kind of situation is not very complex to deal with, the only relatively difficult point is the infinite column query.

In this case I would like to make a simple introduction, for this kind of infinite column query generally there are two ways, one is to use the mechanism of the stack, the other is the use of recursive functions (of course, the recursive function implementation mechanism is also through the implementation of the stack). In the two ways below we are introduced separately.

Recursive function Implementation method

As mentioned above, recursive functions are also implemented by means of stacks, but the underlying processing of stacks is transparent to programmers, and programmers only need to care about the implementation logic of the application. So using recursion to deal with the above problems is easier to understand and more concise.

Now that we use recursive functions, we know that we have to rely on custom functions to look at names. Let me say a little bit about the implementation of the idea, the details we reflected in the code.

For each layer of the function its main work is to find the parent ID is the current ID of the column, find to call their own function again later, will find the ID of the column as the next layer of the parent ID.

The flowchart is as follows

Figure A

Do not know for the above explanation we can understand, it's okay we look directly below the code

<?php/** * Personal blog: Trace Memory Blog * Blog Address: www.onmpw.com * Recursive implementation of infinite pole classification */$channels = array (' ID ' =>1, ' name ' = ' "clothes", ' Parid ' =& gt;0), array (' ID ' =>2, ' name ' = "book", ' Parid ' =>0), array (' ID ' =>3, ' name ' = "T-shirt", ' Parid ' =>1), Array (' Id ' =>4 ', ' name ' = ' pants ', ' parid ' =>1 ', array (' ID ' =>5, ' name ' = ' shoes ', ' Parid ' =>1), array (' ID ' =>6, ' Name ' = ' "Leather Shoes", ' Parid ' =>5), array (' ID ' =>7, ' name ' = ' sneakers ', ' Parid ' =>5), array (' ID ' =>8, ' name ' = ' = ' Nike ", ' Parid ' =>7), array (' ID ' =>9, ' name '" = "Nike", ' Parid ' =>3), array (' ID ' =>10, ' name ' = ' "" Hung "), ' parid ' =  >7), array (' ID ' =>11, ' name ' = "novel", ' Parid ' =>2), array (' ID ' =>12, ' name ' = "Science Fiction", ' Parid ' =>11), Array (' ID ' =>13, ' name ' = ' classic ', ' Parid ' =>11), array (' ID ' =>14, ' name ' = ' literature ', ' Parid ' =>2), Array (' Id ' =>15, ' name ' = ' Classics ', ' Parid ' =>14); $html = Array ();/** * recursively finds nodes with parent ID $parid * @param array $html by Parent-" The structure of the child holds the found node * @param int $parid The specified parent ID * @param array $channels data array * @param int $The DEP traversal depth, initialized to 1 */function getchild (& $html, $parid, $channels, $DEP) {/* * traverse the data, find Parid for the parameter $parid the specified ID */for ($i = 0; $i
 
  
   $channels [$i] [' ID '], ' name ' + $channels [$i] [' name '], ' dep ' + $DEP);    Getchild ($html, $channels [$i] [' ID '], $channels, $DEP + 1); }}}getchild ($html, 0, $channels, 1);? >
  
 

This is the recursive implementation of infinite-level column query core code, combined with the diagram of its implementation process should have a clearer understanding.

Non-recursive, that is, using the stack mechanism to achieve infinite column query

In the above we probably introduced the use of recursive way to achieve infinite columns of the query, below we briefly introduce the non-recursive way. Although it is not possible to use recursive functions, it is necessary to solve this problem by referring to the mechanism of recursive implementation of the structure page of infinite columns.

In school when the teacher said, in fact, the core mechanism of the stack is four words: Advanced after the.

In this for the stack of the mechanism is not much to say, mainly about how to use the stack to achieve infinite level column query.

1. First press the top column into the stack

2. Stack top elements out of the stack

3. Place the stack element into the array, marking its depth (the depth is the depth of the parent column plus 1)

4. Take the element of the stack as the parent column and find its sub-columns

5. Move the found sub-columns into the stack and repeat steps 2

6. If the stack is empty, the process ends;

By translating the above steps, you can translate these steps into PHP code with the following core code:

<?php/** * Personal blog: Trace Memory Blog * Blog Address: www.onmpw.com* use non-recursive, that is, the use of the stack to achieve the column of the Infinite Pole classification query */$channels = array (' ID ' =>1, ' name ' = > "Clothes", ' Parid ' =>0), array (' ID ' =>2, ' name ' = ' book ', ' Parid ' =>0), array (' ID ' =>3, ' name ' = ' t-shirt ', '  Parid ' =>1), array (' ID ' =>4, ' name ' = "Pants", ' Parid ' =>1), array (' ID ' =>5, ' name ' = ' "shoes", ' Parid ' =>1), Array (' ID ' =>6, ' name ' = ' shoes ', ' Parid ' =>5), array (' ID ' =>7, ' name ' = ' sneakers ', ' Parid ' =>5), array (' id ' = >8, ' name ' and ' Nike ', ' Parid ' =>7), array (' ID ' =>9, ' name ' = ' "Nike", ' Parid ' =>3), array (' ID ' =>10, ' name ' = > "Hung Star", ' Parid ' =>7), array (' ID ' =>11, ' name ' = "novel", ' Parid ' =>2), array (' ID ' =>12, ' name ' = ' Science Fiction ') , ' Parid ' =>11), array (' ID ' =>13, ' name ' = ' classical masterpiece ', ' Parid ' =>11), array (' ID ' =>14, ' name ' = ' "Literature", ' Parid ' =&GT;2), array (' ID ' =>15, ' name ' = ' Classics ', ' Parid ' =>14)); $stack = Array ();  Define an empty stack $html = Array (); Used to save the relationship between the columns and the depth of the column */* custom into the stack function */function pushstack (& $stack, $channel, $DEP) {Array_push ($stack, Array (' channel ' = = $channel, ' dep ' + $DEP));} /* * Customize the Stack function */function popstack (& $stack) {return array_pop ($stack);} /* * First press the top column into the stack */foreach ($channels as $key + = $val) {if ($val [' parid '] = = 0) pushstack ($stack, $val, 0);} /* * Stack elements are stacked to find their sub-columns */do{$par = Popstack ($stack);//Put the top element of the stack */* to find the ID of this column as the parent column, put these columns into the stack */for ($i =0; $i
 
  
   $par [' channel '] [' id '], ' name ' and ' = ' $par [' channel '] [' name '], ' dep ' + $par [' dep '];}
while (count ($stack) >0); 
  
 

The above is implemented using non-recursive methods.

Download code: HTTPS://GITHUB.COM/ONMPW/PHPAPP

Summarize

The above two ways have pros and cons, although the implementation of the form above, but given the structure of the infinite column, both the implementation of the mechanism are the same-all by means of the stack to achieve. In the real situation, we need to choose a way to implement according to the real situation.

Articles you may be interested in:

    • PHP+MYSQL enables unlimited class classification | Tree-type display classification relationships
    • PHP Infinite Class Classification learning reference to Ecshop infinite class parsing with detailed comments
    • PHP implements infinite class classification implementation code (recursive method)
    • Table Show Unlimited classes (PHP version)
    • PHP Infinite class classification, Super Simple Infinite class classification, support output tree chart
    • thinkphp Automatic filling method for infinite class classification
    • thinkphp Infinite Classification principle to implement message and reply function example
    • Unlimited class classification for PHP
    • PHP implements an infinite class classification (without using recursion)
    • 2 PHP Unlimited class instance code

http://www.bkjia.com/PHPjc/1108609.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108609.html techarticle PHP Implementation of infinite Class classification query (recursive, non-recursive), categorical query recursive PHP so long, found that the background management system is not a small application module is the classification of columns, a ...

  • 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.