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);

转载于:https://www.cnblogs.com/in-loading/archive/2012/05/24/2516302.html

猜你喜欢

转载自blog.csdn.net/weixin_34220179/article/details/93700342
今日推荐