Example of php smarty second-level classification code and template Loop

Source: Internet
Author: User

The data table structure of level 2 classification is as follows:

Copy codeThe Code is as follows:
The PHP 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
$ 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
{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}

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.