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

yipeiwu_com6年前PHP代码库
二级分类的数据表结构如下:


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}

相关文章

file_get_contents("php://input", "r")实例介绍

file_get_contents("php://input", "r")实例介绍

解释不清,直接上例子index.html复制代码 代码如下:  <form action="action.php" method="post" >  &l...

php中的路径问题与set_include_path使用介绍

first: php中常用的路径 当前文件路径:D:\phpweb\php_example\include_path.php 复制代码 代码如下: 1.dirname(__FILE__)...

详解PHP的Yii框架中日志的相关配置及使用

详解PHP的Yii框架中日志的相关配置及使用

默认的日志是输出到protected/runtime/application.log 如果需要修改那么需要在main.php里面的 components 下面增加log配置,如下:...

Docker 如何布置PHP开发环境

环境部署一直是一个很大的问题,无论是开发环境还是生产环境,但是 Docker 将开发环境和生产环境以轻量级方式打包,提供了一致的环境。极大的提升了开发部署一致性。当然,实际情况并没有这么...

详解php 使用Callable Closure强制指定回调类型

详解php 使用Callable Closure强制指定回调类型 如果一个方法需要接受一个回调方法作为参数,我们可以这样写 <?php function testC...