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
?>