PHP smarty Level Two classification code and template loop example _php tips

Source: Internet
Author: User
Tags custom name
The data table structure of level Two classification is as follows:


The PHP code is as follows

Copy Code code as follows:

/**
@ article classification with two-level classification
@ param int $rootnum--Number of first class categories
@ param int $childnum--Number of categories at level two
@ 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) {
Subclass Array named Childcategory Custom name based on condition
$category [] = Array (' Cateid ' => $parent _value[' Cateid '), ' catename ' => $parent _value[' catename '], ' Childcategory ' =>array ());

Reading a child class
$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
Copy Code code as follows:

$goodscatetree = Array ();
$goodscatetree = Temp_goodstreecate (4,0); Call the classification function (including level two classification) 4--indicates that the first level category displays only 4, 0--indicates that the class two classification is limited in number
$tpl >assign ("Goodscatetree", $goodscatetree); Execute Smarty Engine
$tpl->display-> (INDEX.TPL); Output Smarty Stencil page

TPL template page Output category, such as Index.tpl page
Copy Code code as follows:

{section name=p loop= $goodscatetree}
First Class Category: {$goodscatetree [P].catename}
{section name=c loop= $goodscatetree [P].childcategory}
Class Two: {$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.