PHP smarty Class Two classification code and template cycle Examples _php tutorial

Source: Internet
Author: User
The data table structure for level Two classification is as follows:

Copy CodeThe code is as follows:
The PHP code is as follows
/**
@ article category with two levels of classification
@ param int $rootnum--Number of first class categories
@ param int $childnum--Level two class number
@ 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 according to the situation
$category [] = Array (' cateid ' = = $parent _value[' Cateid '), ' catename ' = = $parent _value[' catename '], ' Childcategory ' =>array ());


Read sub-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
$goodscatetree = Array ();
$goodscatetree = Temp_goodstreecate (4,0); Call classification function (with two levels of classification) 4--indicates that the first class category shows only 4, 0--represents an unlimited number of two classes
$tpl >assign ("Goodscatetree", $goodscatetree); Executing the Smarty engine
$tpl->display-> (INDEX.TPL); Output Smarty Template page


TPL template page output classifications, such as INDEX.TPL pages
{section name=p loop= $goodscatetree}
First-Class classification: {$goodscatetree [P].catename}
{section name=c loop= $goodscatetree [P].childcategory}
Level Two classification: {$goodscatetree [P].childcategory[c].catename}
{/section}
{/section}

http://www.bkjia.com/PHPjc/323429.html www.bkjia.com true http://www.bkjia.com/PHPjc/323429.html techarticle The data table structure of level Two classification is as follows: Copy code code as follows: PHP code is as follows/** @ article category with two levels category @ param int $rootnum--First class number @ param int $childnu ...

  • 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.