三层嵌套循环 值

2016-07-07 15:02 来源:www.chinab4c.com 作者:ecshop专家

我也跟你一样遇到多层嵌套循环的问题,也想请高手请教,个个都忙呀,我现在的解决方法不知合不合理,但能解决问题,我们写循环的时候老被$goods[$row['goods_id']]['url'] 这个两维数组迷惑
我试过我的代码,想多少层都没问题
function get_brand_tree(){
$sql="select brand_id,brand_name FROM ".$GLOBALS['ecs']->table('brand') . "where is_show=1 order by sort_order LIMIT 6";
$res= $GLOBALS['db']->query($sql);
$list = array();
while ($row = $GLOBALS['db']->fetchRow($res)){
$arr['bd_id']=$row['brand_id'];
$arr['name']=$row['brand_name'];
$i++;
$arr['num']=$i;
$arr['brand_id']=get_goods_tree($row['brand_id']);
$list[] = $arr;
}
return $list;
}
function get_goods_tree($bd_id){
$sql = "SELECTg.brand_id,g.goods_id, g.goods_name, g.goods_thumb,g.shop_price ".
" FROM " .$GLOBALS['ecs']->table('goods') . " AS g" .
" WHERE g.brand_id=$bd_id" .
" ORDER BY g.sort_order" .
" LIMIT 4";
$res = $GLOBALS['db']->query($sql);
$list = array();
while ($row = $GLOBALS['db']->fetchRow($res))
{
$arr['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$arr['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
$arr['name']=$row['goods_name'];
$arr['price']=price_format($row['shop_price']);
$arr['brand_id']=get_goods_tree1($row['brand_id']);
$list[] = $arr;
}
return $list;
}
function get_goods_tree1($bd_id){
$sql = "SELECTg.brand_id,g.goods_id, g.goods_name, g.goods_thumb,g.shop_price ".
" FROM " .$GLOBALS['ecs']->table('goods') . " AS g" .
" WHERE g.brand_id=$bd_id" .
" ORDER BY g.sort_order" .
" LIMIT 4";
$res = $GLOBALS['db']->query($sql);
$list = array();
while ($row = $GLOBALS['db']->fetchRow($res))
{
$arr['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$arr['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
$arr['name']=$row['goods_name'];
$arr['price']=price_format($row['shop_price']);
$list[] = $arr;
}
return $list;
}

回答:
不知道你搞定没 ?你想实现啥功能

可以了 代码是对的

看不见啥样的啊