Example of smarty second-level classification code and template Loop

Source: Internet
Author: User

Introduction: This is the second-level classification of smarty.CodeAnd the details page of the template loop example, describes the related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 337303 'rolling = 'no'>

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:

PHP Code
/**
@ Article classification includes secondary classification
@ Param int $ rootnum -- number of first-level 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;
}< br> If (! Isnumber ($ childnum) {
$ childnum = 10;
}< br> $ 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";
}< br> $ parent_cate = $ globals ['db']-> getall ($ parent_ SQL );
foreach ($ parent_cate as $ parent_key => $ parent_value) {
// The array name of the subclass childcategory is the custom name based on the situation.
$ 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}

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/337303.html pageno: 9.

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.