[Category tree] The most efficient way to retrieve a category tree from a database

Source: Internet
Author: User

If you need to perform n * n queries on the database to retrieve the multi-level tree, this is a bad solution.

The solution of plum blossom snow is to retrieve it once and reuse it multiple times. I was inspired. He completed this operation on the client. However, strings are generated on the server.

My approach is to retrieve data from the database twice on the server side and store objects using two cross-indexed arrays. Then, perform regular queries on the two arrays, locate the arrays hierarchically, and generate the category menu string.

I have packaged data tables, CSS, and images.

<? PHP
// $ Q = "select concat_ws ('_', ID, pid) as indexkey, concat_ws ('_', title, Link, published, ordering, access, Params) as valuekey from #__ collector_menu where access> = 0 ";

/**
* The database is searched twice in total, and the concat_ws function is used twice.
* Use regular expressions to search for arrays once (N records) x n times (N x records) * (N x records) (Y records, and so on ).
* For Level 2 classes, there are 2 databases + 1 x n Regular Expressions.
* If a database is used, the database is 1 * n times.
* If array traversal is used, it is a loop of 1 Database (Y records) + N * Y + (Y-N) times.
*
* I don't know which one is more efficient.
*/

$ Q = "select concat_ws ('_', ID, pid) as indexkey, ID, PID, title, Link, published, ordering, access, params from #__ collector_menu where access> = 0 ";
$ Database-> setquery ($ q );
$ Rowsindex = $ database-> loadresultarray (0, 'id', 'indexkey'); // PRT ($ rowsindex );
$ Rows = $ database-> loadassoclist ('indexkey'); // PRT ($ rows );

/* Search all top-level categories */
$ Reg = "# ([0-9] +) _ 0 # I ";
$ Mainindex = preg_grep ($ Reg, $ rowsindex); // PRT ($ mainindex );

/* Find its subcategory for each top-level category */
Foreach ($ mainindex as $ mkey => $ VK ){
/* Retrieve the template */
$ Mainitem = mainitemtpl ();
$ Mainitem = str_replace ('{toptitle}', $ rows [$ VK] ['title'], $ mainitem );
$ Sub = '';

/* Locate the subordinate category in the Index Array */
$ Reg = "# ([0-9] +) _". $ mkey. "# I ";
$ Subindex = preg_grep ($ Reg, $ rowsindex); // PRT ($ subindex );

/* If you find */
If (count ($ subindex )){
/* Combine sub-menus */
$ Sub = '<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0" bgcolor = "# f4fbf4">'. "/N ";
Foreach ($ subindex as $ subkey => $ subv ){
// Echo $ subkey. "=". $ subv. "<br/>"; // PRT ($ rows [$ subv]);

/* Combine sub-menus */

$ Sub. = "<tr>/N ";
$ Sub. = '<TD width = "17%" align = "center" class = "tdline"> </TD> ';
$ Sub. = '<TD width = "83%" class = "tdline"> <a href = "'. $ rows [$ subv] [LINK]. '"> '. $ rows [$ subv] ['title']. "</a> </TD>/N ";
$ Sub. = "</tr> ";

} // The end of the word entry Traversal
$ Sub. = "</table>/N ";
} // End if
 
$ Mainitem = str_replace ('{sub}', $ sub, $ mainitem );
Echo $ mainitem;
}

Function mainitemtpl (){

$ Maintpl = <EOD

<! -- Mainitem -->
<Table width = "120" border = "0" align = "center" cellpadding = "0" cellspacing = "0">
<Tr>
<TD width = "19" Height = "24" align = "center" background = "img/newlinebg2.gif"> </TD>
<TD width = "101" background = "img/newlinebg2.gif" class = "topitem" onclick = "showhide (this)" >{ toptitle} </TD>
</Tr>
<Tr>
<TD colspan = "2" align = "center">
{Sub}
</TD>
</Tr>
</Table>
<! --/Mainitem -->

EOD;
Return $ maintpl;

} // End func

?>

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.