Share the second-level classification code output by the recently written smarty template engine, which is mainly controlled by two-dimensional arrays.
Share the second-level classification code output by the recently written smarty template engine, which is mainly controlled by two-dimensional arrays.
The data table structure of level 2 classification is as follows:
The PHP code is as follows:
The Code is as follows:
/**
@ Document category includes Level 2
@ Param int $ rootnum -- number of hash classes
@ Param int $ childnum -- number of second-level classes
@ Return value array
@ Date 2011.2.24
*/
Function temp_articletreecate ($ rootnum, $ childnum ){
If (! Isnumber ($ rootnum )){
$ Rootnum = 10;
}
If (! Isnumber ($ childnum )){
$ Childnum = 10;
}
$ Category = array ();
$ Parent_ SQL = "SELECT cateid, catename FROM". TABLE_PREFIX. "articlecate WHERE parentid = 0 AND depth = 0 AND flag = 1 ORDER BY orders ASC ";
If (intval ($ rootnum)> 0 ){
$ Parent_ SQL. = "LIMIT $ rootnum ";
}
$ Parent_cate = $ GLOBALS ['db']-> getall ($ parent_ SQL );
Foreach ($ parent_cate as $ parent_key => $ parent_value ){
// The subclass array name is childcategory, which can be customized as needed
$ Category [] = array ('cateid' => $ parent_value ['cateid'], 'catename' => $ parent_value ['catename'], 'childcategory '=> array ());
// Read subclass
$ Child_ SQL = "SELECT cateid, catename FROM". TABLE_PREFIX. "articlecate WHERE parentid =". $ parent_value ['cateid']. "AND flag = 1 ORDER BY orders ASC ";
If (intval ($ childnum)> 0 ){
$ Child_ SQL. = "LIMIT $ childnum ";
}
$ Child_cate = $ GLOBALS ['db']-> getall ($ child_ SQL );
Foreach ($ child_cate as $ child_key => $ child_value ){
$ Category [count ($ category)-1] ['childcategory '] [] = array ('cateid' => $ child_value ['cateid'], 'catename' => $ child_value ['catename']);
}
}
Return $ category;
}
PHP page call classification, such as index. php
The Code is as follows:
$ Goodscatetree = array ();
$ Goodscatetree = temp_goodstreecate (); // call the classification function (including level-2 classification) 4 -- indicates that only four level-1 classes are displayed, and 0 indicates that the number of level-2 classes is unlimited.
$ Tpl> assign ("goodscatetree", $ goodscatetree); // executes the smarty Engine
$ Tpl-> display-> (index. tpl); // outputs the smarty template page.
TPL template page output category, such as index. tpl page
The Code is as follows:
{Section name = p loop = $ goodscatetree}
Level 1 category: {$ goodscatetree [p]. catename}
{Section name = c loop = $ goodscatetree [p]. childcategory}
Level 2 Category: {$ goodscatetree [p]. childcategory [c]. catename}
{/Section}
{/Section}