Home >Backend Development >PHP Tutorial > 求教怎么读取无限级分类

求教怎么读取无限级分类

WBOY
WBOYOriginal
2016-06-13 12:52:45755browse

求教如何读取无限级分类
就是一个简单的无限级分类

categoryTable结构如下:
id  name  pid

现在需要把所有分类读出来  转成array() 或者json的格式

小弟在网上找了不少列子看  但对php不是很熟  很多都用不了

像$db->fetch_array(),$db->fetch_array()这些 一用就出错

现在向各位大神求教个简单实用的  望不吝赐教  小弟感激不尽

对了  小弟用的是CodeIgniter 的框架  先谢谢各位大神了!~


------解决方案--------------------
<br />
function getCat($pid,$lnum=1,$str='',$level=0){<br />
	static  $catlist=array();<br />
	$sql= "select * categoryTable where pid=$pid";<br />
	$rst = mysql_query($sql);<br />
	while($row=mysql_fetch_assoc($rst)){<br />
		$l = str_repeat(" &nbsp  ",$level*$lnum);<br />
		$catname =$l.($pid==0?'':$str).$row['cat_name'];<br />
		$catlist[]=array('catid'=>$row['cat_id'],<br />
				'catname'=>$catname<br />
		);<br />
		getCat($row['cat_id'],$lnum,$str,$level+1);<br />
<br />
	}<br />
	return $catlist;<br />
}<br />

------解决方案--------------------
http://bbs.csdn.net/topics/390364669    
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