Php practices: Using recursion to read unlimited product categories

Source: Internet
Author: User
: This article describes how to use recursive reading of unlimited product categories in php practice. For more information about PHP tutorials, see. Recently, when I was working on a mall project, I needed to retrieve the data in the category table. in the category table, I used a pid (parent class id) to associate each category, to organize and read the two-dimensional data into a tree format, we encapsulate the following method in the project.

/*** [TreeCats description] * @ param [array] $ arr [original unordered array] * @ param [array] $ limit [the first element indicates the start of recursive parent_id, the default value is 0. The second element indicates removing the element and the child element cat_id. If no value is passed, the default value is 0] * @ param integer $ level [function call depth] * @ return [array] [sorted array] */publicfunctiontreeCats ($ arr, $ limit, $ level = 0) {$ rec = array (); // first remove the value foreach ($ arras $ key => $ value) corresponding to the cat_id) {if ($ value ['cat _ id']! = $ Limit ['CD']) {$ arr_new [$ key] = $ value ;}/// traverse the new array foreach ($ arr_newas $ key => $ value) {if ($ value ['parent _ id'] = $ limit ["pd"] & $ value ['parent _ id']! = $ Limit ["cd"]) {$ value ['level'] = $ level; $ rec [] = $ value; // generate an array for the next loop $ next_limit = array ("pd" => $ value ['cat _ id'], "cd" => $ limit ["cd"]); $ rec = array_merge ($ rec, $ this-> treeCats ($ arr, $ next_limit, $ level + 1);} return $ rec ;}

The application scenario of setting $ limit here is that if the parent category of this category is changed to its original subcategory, the category branch will be lost, at this time, we should remove its subclass from the optional classes.

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above introduces the use of recursive reading of unlimited product categories in php practice, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

    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.