運用循環實現PHP分類列表
作者:佚名
PHP分類列表可以通過foreach循環向數組中插入一個索引來實現。具體的操作方法我們在文章中有詳細的介紹,希望對大家有所幫助。
PHP語言的初學者們對于如何運用各個函數來實現不同的功能非常感興趣。PHP分類列表的實現簡單的說就是***分類下面列出屬于這個***分類的所有子分類。#t#
html代碼...用到兩個foreach 循環
- < div class="modContent">
- < %foreach from=$sort item=s%>
- < div class="categories">
- < acronym class="icon">< /acronym>
- < a href="category.php?sort_id=
< %$s.sort_id%>"> - < strong>< %$s.sort_name%>< /strong>
- < /a>< br />
- < %foreach from=$s.zi item=z%>
- < a href="category.php?sort_id=
< %$z.sort_id%>" - title="< %$z.sort_name%>">
- < %$z.sort_name%>
- < /a>
- < %/foreach%>
- < /div>
- < %/foreach%>
- < /div>
PHP分類列表之PHP代碼 簡單的說就是利用foreach 循環一個數組然后在這個數組里插入一個索引 索引對的直就是每次查詢的所有子分類
- //商品分類
- $sql="SELECT * FROM `hcl_sort`
WHERE `parent_id`=0 ORDER BY
`sort_id` DESC";- $sort=$db->getAll($sql);
- foreach ($sort as $key=>$val){
- $sort_id=$sort[$key]['sort_id'];
- $sql="SELECT * FROM `hcl_sort`
WHERE `parent_id`={$sort_id}";- $zi=$db->getAll($sql);
- $sort[$key]['zi']=$zi;
- }
- $tpl->assign('sort',$sort);
以上代碼就是PHP分類列表的具體解決方法。
責任編輯:曹凱
來源:
百度博客