PHP unlimited classification implementation (no recursion is used)

Source: Internet
Author: User
Unlimited classification is often used in development, such as Department structure and document classification. The difficulty of an unlimited classification lies in output and query. For example, the document category is output to the ul list form, and the article contained in all categories under category A is searched. 1. Implementation Principles: several common implementations are introduced in the article "how to implement an unlimited classification ".

Unlimited classification is often used in development, such as Department structure and document classification. The difficulty of unlimited classification lies in output and query. For example, the document category is output in the ul list form, and the article contained in all categories under category A is searched. 1. Implementation principles I have introduced several common practices in "Unlimited classification implementation ideas"

UnlimitedLevelCategoryOften used during developmentUseFor example, Department structure and articleCategory.UnlimitedLevelCategoryThe difficulty lies in "output" and "query", for example

  • PostCategoryThe output is
      List form;
    • SearchCategoryAllCategoryIncluded articles.
    1.ImplementationPrinciple

    In 《UnlimitedLevelCategoryImplementationThis article introduces several commonImplementationMethods have their own advantages and disadvantages. The data structure of "improved forward traversal Tree" facilitates output and query,CategoryIt is somewhat complicated than conventional understanding.

    2. Data Structure
    Id Fid Title
    1 0 China
    2 1 Jiangsu
    3 1 Anhui Province
    4 8 Jiangyin
    5 3 Wuhu
    6 3 Hefei
    7 3 Bengbu
    8 2 Wuxi
    
        1, 'fid' => 0, 'title' => 'China'), array ('id' => 2, 'fid' => 1, 'title' => 'jiangsu '), array ('id' => 3, 'fid' => 1, 'title' => 'anhui '), array ('id' => 4, 'fid' => 8, 'title' => 'jiangyin '), array ('id' => 5, 'fid' => 3, 'title' => 'wuhu '), array ('id' => 6, 'fid' => 3, 'title' => 'hefei '), array ('id' => 7, 'fid' => 3, 'title' => 'bengbu '), array ('id' => 8, 'fid' => 8, 'title' => 'wuxi ');?>

    VariousCategoryThrough the parent class id (that is, fid) to formCategoryTree. Note the following points during the series:CategoryThe fid of A cannot be the id of its subclass.

    InUseThe most common algorithm for outputting such data structures is "recursion". friends familiar with PHP certainly know that PHP is not good at recursion and has limited recursion times (about 100 times, operating System and configuration vary ).

    Because all recursion is acceptableUseLoopImplementationAccording to the characteristics of the PHP language, this article compiled a set ofUnlimitedLevel"CategoryCompared with recursionImplementationHigher efficiency

    3. Output ul List format

    Output the above data as the following HTML

    
        
        
    • Jiangsu

      • Wuxi

        • Jiangyin

    • Anhui Province

      • Wuhu

      • Hefei

      • Bengbu

    This HTML structure is on the front endUse(UseJavaScript and CSS to construct a foldable tree) is very convenient. DetailsImplementationThe procedure is as follows:

    
        
        
    4. Output option List format
        Jiangsu    Wuxi    Jiangyin    Anhui Province    Wuhu    Hefei    Bengbu

    DetailsImplementationThe procedure is as follows:

    
         '. Str_repeat ("", $ op ['Demo'] * 4). $ op ['title'].' <;/option> ';}?> <;/Select>5. FindCategoryAll subclasses
         6. FindCategoryAll parent classes
         7. Related functions
         = 0; $ I --) {$ node = $ data [$ I]; if ($ node ['fid'] = $ fid) {array_splice ($ data, $ I, 1); $ result [] = $ node ['id']; $ cids [] = $ node ['id']; $ flag = true ;}}} $ fids = $ cids;} while ($ flag = true); return $ result;} function get_tree_parent ($ data, $ id) {$ result = array (); $ obj = array (); foreach ($ data as $ node) {$ obj [$ node ['id'] = $ node ;} $ value = isset ($ obj [$ id])? $ Obj [$ id]: null; while ($ value) {$ id = null; foreach ($ data as $ node) {if ($ node ['id'] = $ value ['fid']) {$ id = $ node ['id']; $ result [] = $ node ['id']; break ;}} if ($ id = null) {$ result [] = $ value ['fid'];} $ value = isset ($ obj [$ id])? $ Obj [$ id]: null;} unset ($ obj); return $ result;} function get_tree_ul ($ data, $ fid) {$ stack = array ($ fid ); $ child = array (); $ added_left = array (); $ added_right = array (); $ html_left = array (); $ html_right = array (); $ obj = array (); $ loop = 0; foreach ($ data as $ node) {$ pid = $ node ['fid']; if (! Isset ($ child [$ pid]) {$ child [$ pid] = array ();} array_push ($ child [$ pid], $ node ['id']); $ obj [$ node ['id'] = $ node;} while (count ($ stack)> 0) {$ id = $ stack [0]; $ flag = false; $ node = isset ($ obj [$ id])? $ Obj [$ id]: null; if (isset ($ child [$ id]) {$ cids = $ child [$ id]; $ length = count ($ cids); for ($ I = $ length-1; $ I >=0; $ I --) {array_unshift ($ stack, $ cids [$ I]);} $ obj [$ cids [$ length-1] ['islastchild '] = true; $ obj [$ cids [0] ['isfirstchild '] = true; $ flag = true;} if ($ id! = $ Fid & $ node &&! Isset ($ added_left [$ id]) {if (isset ($ node ['isfirstchild ']) & isset ($ node ['islastchild']) {$ html_left [] ='';} Else if (isset ($ node ['isfirstchild']) {$ html_left [] ='';} Else if (isset ($ node ['islastchild']) {$ html_left [] ='';} Else {$ html_left [] ='';} $ Html_left [] = ($ flag = true )? "{$ Node ['title']}":"{$ Node ['title']}"; $ Added_left [$ id] = true;} if ($ id! = $ Fid & $ node &&! Isset ($ added_right [$ id]) {$ html_right [] = ($ flag = true )? '':''; $ Added_right [$ id] = true;} if ($ flag = false) {if ($ node) {$ cids = $ child [$ node ['fid']; for ($ I = count ($ cids)-1; $ I >=0; $ I --) {if ($ cids [$ I] ==$ id) {array_splice ($ child [$ node ['fid'], $ I, 1); break ;}} if (count ($ child [$ node ['fid']) = 0) {$ child [$ node ['fid'] = null ;}} array_push ($ html_left, array_pop ($ html_right); array_shift ($ stack) ;}$ loop ++; if ($ loop> 5000) return $ html_l Eft;} unset ($ child); unset ($ obj); return implode ('', $ html_left);} function get_tree_option ($ data, $ fid) {$ stack = array ($ fid); $ child = array (); $ added = array (); $ options = array (); $ obj = array (); $ loop = 0; $ depth =-1; foreach ($ data as $ node) {$ pid = $ node ['fid']; if (! Isset ($ child [$ pid]) {$ child [$ pid] = array ();} array_push ($ child [$ pid], $ node ['id']); $ obj [$ node ['id'] = $ node;} while (count ($ stack)> 0) {$ id = $ stack [0]; $ flag = false; $ node = isset ($ obj [$ id])? $ Obj [$ id]: null; if (isset ($ child [$ id]) {for ($ I = count ($ child [$ id])-1; $ I> = 0; $ I --) {array_unshift ($ stack, $ child [$ id] [$ I]);} $ flag = true;} if ($ id! = $ Fid & $ node &&! Isset ($ added [$ id]) {$ node ['dest'] = $ depth; $ options [] = $ node; $ added [$ id] = true ;} if ($ flag = true) {$ depth ++;} else {if ($ node) {for ($ I = count ($ child [$ node ['fid'])-1; $ I >=0; $ I --) {if ($ child [$ node ['fid'] [$ I] ==$ id) {array_splice ($ child [$ node ['fid'], $ I, 1); break; }}if (count ($ child [$ node ['fid']) = 0) {$ child [$ node ['fid'] = null; $ depth -- ;}} array_shift ($ stack) ;}$ loo P ++; if ($ loop> 5000) return $ options;} unset ($ child); unset ($ obj); return $ options ;}?>

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.