php 递归 无限级分类 返回数组
header("Content-type:text/html;charset=utf-8");
$conn = mysql_connect('localhost','root','123456');
mysql_select_db('test');
mysql_query("set names 'utf8'");
function getCate($pid = 0)
{
$sql = "select * from cate where pid=".$pid;
$res = mysql_query($sql);
if($res)
{
while($row = mysql_fetch_assoc($res)){
$categories[] = $row;
}
}
if(0 < count($categories))
{
for($i = 0; $i < count($categories); $i++)
{
$categories[$i]['child'] = getCate($categories[$i]['id']);
}
}
return $categories;
}
$cate = getCate(0);