Home  >  Article  >  Backend Development  >  PHP smarty two-level classification code and template loop example_PHP tutorial

PHP smarty two-level classification code and template loop example_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:29:31736browse

The data table structure of the secondary classification is as follows:

PHP smarty two-level classification code and template loop example_PHP tutorial

Copy code The code is as follows:

The PHP code is as follows
/**
@ Article classification includes second-level classification
@ param int $rootnum -- the number of first-level classifications
@ param int $childnum -- the number of second-level classifications
@ 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 is named childcategory. Customize the name according to 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 calls classification, such as index.php
$goodscatetree = array();
$goodscatetree = temp_goodstreecate(4,0); //Call classification function (including secondary classification) 4--means Only 4 first-level categories are displayed, 0--indicates an unlimited number of second-level categories
$tpl>assign("goodscatetree",$goodscatetree); //Execute the smarty engine
$tpl->display-> ;(index.tpl); //Output smarty template page


TPL template page output category, such as index.tpl page
{section name=p loop=$goodscatetree}
一First-level classification: {$goodscatetree[p].catename}
{section name=c loop=$goodscatetree[p].childcategory}
Second-level classification: {$goodscatetree[p].childcategory[c].catename }
{/section}
{/section}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323429.htmlTechArticleThe data table structure of the secondary classification is as follows: Copy the code as follows: The PHP code is as follows/** @ Article classification contains Second-level classification @ param int $rootnum -- Number of first-level classification @ param int $childnu...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn