Home  >  Article  >  php教程  >  运用循环实现PHP分类列表

运用循环实现PHP分类列表

WBOY
WBOYOriginal
2016-06-13 11:10:201275browse

html代码...用到两个foreach 循环

  1.  div class="modContent"> 
  2.  %foreach from=$sort item=s%> 
  3.  div class="categories"> 
  4.  acronym class="icon"> /acronym>
  5.  a href="category.php?sort_id=
    "
    >
  6.  strong> %$s.sort_name%> /strong>
  7.  /a> br /> 
  8.  %foreach from=$s.zi item=z%> 
  9.  a href="category.php?sort_id=
    "
     
  10. title="">
  11.  %$z.sort_name%>
  12.  /a>    
  13.  %/foreach%> 
  14.  /div>   
  15.  %/foreach%> 
  16.  /div> 

PHP分类列表之PHP代码 简单的说就是利用foreach 循环一个数组然后在这个数组里插入一个索引 索引对的直就是每次查询的所有子分类

<ol class="dp-xml">
<li class="alt"><span><span>//商品分类  </span></span></li>
<li>
<span>$</span><span class="attribute">sql</span><span>=</span><span class="attribute-value">"SELECT * FROM `hcl_sort` <br>WHERE `parent_id`=0 ORDER BY <br>`sort_id` DESC"</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">sort</span><span>=$db-</span><span class="tag">></span><span>getAll($sql);  </span>
</li>
<li>
<span>foreach ($sort as $</span><span class="attribute">key</span><span>=</span><span class="tag">></span><span>$val){  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">sort_id</span><span>=$sort[$key]['sort_id'];  </span>
</li>
<li>
<span>$</span><span class="attribute">sql</span><span>=</span><span class="attribute-value">"SELECT * FROM `hcl_sort`<br> WHERE `parent_id`={$sort_id}"</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">zi</span><span>=$db-</span><span class="tag">></span><span>getAll($sql);  </span>
</li>
<li><span>$sort[$key]['zi']=$zi;  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span>$tpl-</span><span class="tag">></span><span>assign('sort',$sort); </span>
</li>
</ol>

以上代码就是PHP分类列表的具体解决方法。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn