一聚教程网:一个值得你收藏的教程网站

热门教程

smarty 多级分类实现方法

时间:2022-06-25 01:00:10 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码
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 代码

 

 代码如下 复制代码

    {foreach name=cat from=$class item=cat}
  • {$cat.name}[{$cat.id}]


    • {foreach name=subcat from=$subclass item=subcat}
      {if $subcat.parent_id==$cat.id}
    • {$subcat.name}[{$subcat.parent_id}]

    • {/if}
      {/foreach}

    {/foreach}


运行结果
php mysql smarty 多级分类

热门栏目