zoukankan      html  css  js  c++  java
  • smarty二级分类代码和模版循环例子

    简介:这是smarty二级分类代码和模版循环例子的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

    class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=337303' scrolling='no'>

    分享下最近写的smarty模版引擎输出二级分类代码,主要是靠二维数组进行控制输出。


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



    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}

    爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

    http://biancheng.dnbcw.info/php/337303.html pageNo:9
  • 相关阅读:
    python 监听文件夹下的文件,将文本内容写入kafka,支持断电续传 (docker 发布)
    python监控目录和文件变化
    Python 单例
    Python 面向对象 继承
    Python 面向对象 多态
    Python 面向对象 类属性和类方法
    Python 基础2
    Python 面向对象
    【bird-front】全自动数据表格组件bird-grid
    【bird-java】bird-java系列文章汇总
  • 原文地址:https://www.cnblogs.com/ooooo/p/2247031.html
Copyright © 2011-2022 走看看