<?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'); ?>
<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程序设计有所帮助。