smarty实现多级分类的方法

yipeiwu_com7年前PHP代码库

本文实例讲述了smarty实现多级分类的方法。分享给大家供大家参考。具体分析如下:

这里简单的介绍一下利用php smarty 多级分类读出与循环方法,单循环很简单,但是多级就要复杂一点,下面我们来看看,感兴趣的朋友可以看来一下:

复制代码 代码如下:
<?php
$result=mysql_query("SELECT id,description,parent_id,name,sort_order FROM article_categoey WHERE parent_id=0 order by id");
while ($row = mysql_fetch_array($result)){
$class[]=$row;
//$row['id']是分类id,parent_id是子分类所属id
$result_sub=mysql_query("SELECT id,description,parent_id,name,sort_order FROM article_categoey WHERE parent_id='".$row['id']."' order by id");
while ($row_sub = mysql_fetch_array($result_sub)) {
$subclass[]=$row_sub;
}
$smarty->assign("subclass", $subclass);
}
$smarty->assign("class", $class);
$smarty->display('articles.html');
?>

HTML 代码如下:
复制代码 代码如下:
<ul>
{foreach name=cat from=$class item=cat}
<li><strong>{$cat.name}</strong>[{$cat.id}]</li>
<ul style="padding-left:20px;">
{foreach name=subcat from=$subclass item=subcat}
{if $subcat.parent_id==$cat.id}
<li>{$subcat.name}[{$subcat.parent_id}]</li>
{/if}
{/foreach}
</ul>
{/foreach}
</ul>

运行结果如下图所示:

希望本文所述对大家的PHP程序设计有所帮助。

相关文章

PHP Parse Error: syntax error, unexpected $end 错误的解决办法

这几天写php程序,感觉很多地方不如asp,asp.Net,jsp顺手,比如session使用先得session_start();,文件跳转header用的也不方便.... 也许是不熟悉...

PHP+Oracle本地开发环境搭建方法详解

安装instant client 首先,是从https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html下载...

php中变量及部分适用方法

变量:$_SERVER,         在   PHP ...

php配置php-fpm启动参数及配置详解

约定几个目录 /usr/local/php/sbin/php-fpm/usr/local/php/etc/php-fpm.conf/usr/local/php/etc/php.ini一,...

解析php中memcache的应用

所需环境:php 5.3.3apache 2.2.7mysql 5.5.8相关文档下载:点击下载解压Memcached_1.2.5文档,cmd下执行memcached.exe -d -i...