smarty 多级分类实现方法

作者:袖梨 2022-06-25
 代码如下 复制代码
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 多级分类

相关文章

精彩推荐