循环嵌套问题求助

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

这个说实话· ·不懂哦···

回答:
错误还是比较多的:

function index_get_newjiadao()
{
$type = $GLOBALS['db']->getAll("SELECT * F ...
ecshopkaifa 发表于 2011-12-25 07:14



我现在要在这个嵌套分类循环基础上面增加一个商品展示程序比如:有两个一级分类 A 和B在A 分类下面又有小分类 A_1,A_2,A_3。B分类下面有 B_1,B_2,B_3。在上面嵌套分类循环下面我要显示出A分类下面小分类的产品 同时要包括分类A的产品, B分类同理。求解。我自己在那基础上写了一个 但是貌似不行 不知道哪里写错了 我贴出来 :
function index_get_newjiadao22()
{
$type = $GLOBALS['db']->getAll("SELECT * FROM ecs_category WHERE parent_id=0");
foreach($type AS $key => $typename){
$hid = $typename['cat_id'];
$typeone[$hid] = $GLOBALS['db']->getAll("SELECT * FROM ecs_category WHERE parent_id=$hid");
$sa = $typeone[$hid];
foreach($sa AS $sva => $typegood){
$gid = $typegood['cat_id'];
$goods[$gid] = $GLOBALS['db']->getAll("SELECT goods_id,cat_id,goods_name,market_price,shop_price,goods_thumb FROM ecs_goods WHERE cat_id=$gid AND is_delete=0 ORDER BY goods_id DESC LIMIT 0,3");
var_dump($goods);
}

}

$sves['type'] = $type;
$sves['typeone'] = $typeone;
$sves['goods'] = $goods;
return $sves;

}


模板页代码为:
<!--{foreach from=$type item=type}-->
<div class="rr">
<div class="dd1">
<div class="dd1_1">
<div class="dd1_1_1 lf">{$type.cat_name}</div>
<div class="dd1_1_2 lf">
<!--{foreach from=$typeone[$type.cat_id] item=typeone}-->
<a href="category.php?id={$typeone.cat_id}">{$typeone.cat_name}</a>|
<!--{/foreach}-->
</div>
<div class="dd1_1_3 rg"><a href="category.php?id={$type.cat_id}">查看更多>></a></div>
<div class="clear"></div>
</div>
</div>
<div class="rr1">
<div class="rr1_1 lf">
<a href="{$type.cat_desc}"><img src="{$type.keywords}" width="372" height="424" style="border:1px solid #c38c86;" class="imgs"></a>
</div>
<div class="rr1_2 lf">
<ul>
<!--{foreach from=$goods[$typeone.cat_id] item=goods key=t}-->
<li>
<div class="divr1"><a href="goods.php?id={$goods.goods_id}"><img src="{$goods.goods_thumb}" width="170" height="141" style="border:1px solid #e0e0e1;"></a></div>
<div class="divr2"><a href="">333{$goods.goods_name}</a></div>
<div class="divr3"><span class="spanr1">{$goods.market_price}</span><span class="spanr2">{$goods.shop_price}</span></div>
</li>
<!--{/foreach}-->
<div class="clear"></div>
</ul>
</div>
<div class="clear"></div>
</div>
</div>
<!--{/foreach}-->

麻烦看看哪里有问题 。谢谢

错误还是比较多的:

function index_get_newjiadao()
{
$type = $GLOBALS['db']->getAll("SELECT * F ...
ecshopkaifa 发表于 2011-12-25 07:14



我现在要在这个嵌套分类循环基础上面增加一个商品展示程序比如:有两个一级分类 A 和B在A 分类下面又有小分类 A_1,A_2,A_3。B分类下面有 B_1,B_2,B_3。在上面嵌套分类循环下面我要显示出A分类下面小分类的产品 同时要包括分类A的产品, B分类同理。求解。我自己在那基础上写了一个 但是貌似不行 不知道哪里写错了 我贴出来 :
function index_get_newjiadao22()
{
$type = $GLOBALS['db']->getAll("SELECT * FROM ecs_category WHERE parent_id=0");
foreach($type AS $key => $typename){
$hid = $typename['cat_id'];
$typeone[$hid] = $GLOBALS['db']->getAll("SELECT * FROM ecs_category WHERE parent_id=$hid");
$sa = $typeone[$hid];
foreach($sa AS $sva => $typegood){
$gid = $typegood['cat_id'];
$goods[$gid] = $GLOBALS['db']->getAll("SELECT goods_id,cat_id,goods_name,market_price,shop_price,goods_thumb FROM ecs_goods WHERE cat_id=$gid AND is_delete=0 ORDER BY goods_id DESC LIMIT 0,3");
var_dump($goods);
}

}

$sves['type'] = $type;
$sves['typeone'] = $typeone;
$sves['goods'] = $goods;
return $sves;

}


模板页代码为:
<!--{foreach from=$type item=type}-->
<div class="rr">
<div class="dd1">
<div class="dd1_1">
<div class="dd1_1_1 lf">{$type.cat_name}</div>
<div class="dd1_1_2 lf">
<!--{foreach from=$typeone[$type.cat_id] item=typeone}-->
<a href="category.php?id={$typeone.cat_id}">{$typeone.cat_name}</a>|
<!--{/foreach}-->
</div>
<div class="dd1_1_3 rg"><a href="category.php?id={$type.cat_id}">查看更多>></a></div>
<div class="clear"></div>
</div>
</div>
<div class="rr1">
<div class="rr1_1 lf">
<a href="{$type.cat_desc}"><img src="{$type.keywords}" width="372" height="424" style="border:1px solid #c38c86;" class="imgs"></a>
</div>
<div class="rr1_2 lf">
<ul>
<!--{foreach from=$goods[$typeone.cat_id] item=goods key=t}-->
<li>
<div class="divr1"><a href="goods.php?id={$goods.goods_id}"><img src="{$goods.goods_thumb}" width="170" height="141" style="border:1px solid #e0e0e1;"></a></div>
<div class="divr2"><a href="">333{$goods.goods_name}</a></div>
<div class="divr3"><span class="spanr1">{$goods.market_price}</span><span class="spanr2">{$goods.shop_price}</span></div>
</li>
<!--{/foreach}-->
<div class="clear"></div>
</ul>
</div>
<div class="clear"></div>
</div>
</div>
<!--{/foreach}-->

麻烦看看哪里有问题 。谢谢

错误还是比较多的:

function index_get_newjiadao()
{
$type = $GLOBALS['db']->getAll("SELECT * F ...
ecshopkaifa 发表于 2011-12-25 07:14



我现在要在这个嵌套分类循环基础上面增加一个商品展示程序比如:有两个一级分类 A 和B在A 分类下面又有小分类 A_1,A_2,A_3。B分类下面有 B_1,B_2,B_3。在上面嵌套分类循环下面我要显示出A分类下面小分类的产品 同时要包括分类A的产品, B分类同理。求解。我自己在那基础上写了一个 但是貌似不行 不知道哪里写错了 我贴出来 :
function index_get_newjiadao22()
{
$type = $GLOBALS['db']->getAll("SELECT * FROM ecs_category WHERE parent_id=0");
foreach($type AS $key => $typename){
$hid = $typename['cat_id'];
$typeone[$hid] = $GLOBALS['db']->getAll("SELECT * FROM ecs_category WHERE parent_id=$hid");
$sa = $typeone[$hid];
foreach($sa AS $sva => $typegood){
$gid = $typegood['cat_id'];
$goods[$gid] = $GLOBALS['db']->getAll("SELECT goods_id,cat_id,goods_name,market_price,shop_price,goods_thumb FROM ecs_goods WHERE cat_id=$gid AND is_delete=0 ORDER BY goods_id DESC LIMIT 0,3");
var_dump($goods);
}

}

$sves['type'] = $type;
$sves['typeone'] = $typeone;
$sves['goods'] = $goods;
return $sves;

}


模板页代码为:
<!--{foreach from=$type item=type}-->
<div class="rr">
<div class="dd1">
<div class="dd1_1">
<div class="dd1_1_1 lf">{$type.cat_name}</div>
<div class="dd1_1_2 lf">
<!--{foreach from=$typeone[$type.cat_id] item=typeone}-->
<a href="category.php?id={$typeone.cat_id}">{$typeone.cat_name}</a>|
<!--{/foreach}-->
</div>
<div class="dd1_1_3 rg"><a href="category.php?id={$type.cat_id}">查看更多>></a></div>
<div class="clear"></div>
</div>
</div>
<div class="rr1">
<div class="rr1_1 lf">
<a href="{$type.cat_desc}"><img src="{$type.keywords}" width="372" height="424" style="border:1px solid #c38c86;" class="imgs"></a>
</div>
<div class="rr1_2 lf">
<ul>
<!--{foreach from=$goods[$typeone.cat_id] item=goods key=t}-->
<li>
<div class="divr1"><a href="goods.php?id={$goods.goods_id}"><img src="{$goods.goods_thumb}" width="170" height="141" style="border:1px solid #e0e0e1;"></a></div>
<div class="divr2"><a href="">333{$goods.goods_name}</a></div>
<div class="divr3"><span class="spanr1">{$goods.market_price}</span><span class="spanr2">{$goods.shop_price}</span></div>
</li>
<!--{/foreach}-->
<div class="clear"></div>
</ul>
</div>
<div class="clear"></div>
</div>
</div>
<!--{/foreach}-->

麻烦看看哪里有问题 。谢谢

逻辑就是父接点的ID做为 key去组成数组