二级分类php代码,php smarty 二级分类代码和模版循环例子

该博客介绍了如何使用PHP创建一个包含二级分类的文章数据结构,并展示了如何在页面中调用和展示这些分类。通过`temp_articletreecate`函数,可以根据指定的一级和二级分类数量从数据库获取分类数据。然后,使用SMARTY模板引擎在页面上遍历并显示这些分类。示例代码展示了如何限制一级分类的数量,同时允许无限数量的二级分类。
摘要由CSDN通过智能技术生成

二级分类的数据表结构如下:

20110616234834760.png

PHP代码如下

/**

@ 文章分类 含二级分类

@ param int $rootnum -- 一级分类数量

@ param int $childnum -- 二级分类数量

@ 返回值 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){

//子类数组名为 childcategory 根据情况自定义名称

$category[] = array('cateid'=>$parent_value['cateid'],'catename'=>$parent_value['catename'],'childcategory'=>array());

//读取子类

$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页面调用分类,如index.php

$goodscatetree = array();

$goodscatetree = temp_goodstreecate(4,0); //调用分类函数(含二级分类)4--表示一级分类只显示4个,0--表示二级分类不限数量

$tpl>assign("goodscatetree",$goodscatetree); //执行smarty引擎

$tpl->display->(index.tpl); //输出smarty模版页面

TPL模版页面输出分类,如index.tpl页面

{section name=p loop=$goodscatetree}

一级分类:{$goodscatetree[p].catename}

{section name=c loop=$goodscatetree[p].childcategory}

二级分类:{$goodscatetree[p].childcategory[c].catename}

{/section}

{/section}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值