ecshop如何遍历自定义类别下对应的商品

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

未命名.jpg

想将精品推荐新品上架热销商品换成自定义类别现在类别遍历出来了,可对应的商品不知道如何遍历,

现在只修改过精品推荐的lbi 其他的文件都没有动

recommend_best.lbi

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  2. <!-- {if $best_goods} -->

  3. <!--{foreach from=$categories item=cat}-->
  4. <div class="box box_1">

  5. <a href="../search.php?intro=best" >精品推荐</a>

  6. <div class="cat_tit"> <span><a href="{$cat.url}">{$cat.name|escape:html}</a></span> <ahref="../search.php?intro=best">更多>></a></div>
  7. <div class="goods_cat">
  8. <div class="clearfix goodsBox" >

  9. <!--{foreach from=$cat_list item=goods}-->这一句该如何写才能遍历对应的商品

  10. <div class="goodsItem ">
  11. <a href="{$goods.url}"><img src="{$goods.thumb}" alt="{$goods.name|escape:html}" class="goodsimg" /></a><br />
  12. <p><a href="{$goods.url}" title="{$goods.name|escape:html}">{$goods.short_name|escape:html}</a></p>
  13. <!-- {if $goods.promote_price neq ""} -->
  14. <font class="shop_s">{$goods.promote_price}</font>
  15. <!-- {else}-->
  16. <font class="shop_s">{$goods.shop_price}</font>
  17. <!--{/if}-->
  18. </div>

  19. <!--{/foreach}-->

  20. </div>
  21. </div>
  22. </div>
  23. <div class="blank"></div>
  24. <!--{/foreach}-->
  25. <!-- {/if} -->
复制代码

回答:
求答案卡了好几天了

其他的文件都动没动?




是的大哥急死我了 就是循环出当前类别下商品语句 我不会写帖子代码那我有标记

这需要php和模板文件匹配,可以加些打印代码查看下

以下的方式我沒實測過,不確定是否正確,要做之前請先做好備份:

打開includes/lib_goods.php,大約在289行附近找到
  1. //取出所有符合条件的商品数据,并将结果存入对应的推荐类型数组中
  2. $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
  3. "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
  4. "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img, RAND() AS rnd " .
  5. 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
  6. "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
  7. "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ";
  8. $type_merge = array_merge($type_array['new'], $type_array['best'], $type_array['hot']);
  9. $type_merge = array_unique($type_merge);
  10. $sql .= ' WHERE g.goods_id ' . db_create_in($type_merge);
  11. $sql .= ' ORDER BY g.sort_order, g.last_update DESC';

  12. $result = $GLOBALS['db']->getAll($sql);
  13. foreach ($result AS $idx => $row)
  14. {
  15. if ($row['promote_price'] > 0)
  16. {
  17. $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
  18. $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
  19. }
  20. else
  21. {
  22. $goods[$idx]['promote_price'] = '';
  23. }

  24. $goods[$idx]['id']= $row['goods_id'];
  25. $goods[$idx]['name']= $row['goods_name'];
  26. $goods[$idx]['brief']= $row['goods_brief'];
  27. $goods[$idx]['brand_name']= isset($goods_data['brand'][$row['goods_id']]) ? $goods_data['brand'][$row['goods_id']] : '';
  28. $goods[$idx]['goods_style_name']= add_style($row['goods_name'],$row['goods_name_style']);

  29. $goods[$idx]['short_name']= $GLOBALS['_CFG']['goods_name_length'] > 0 ?
  30. sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
  31. $goods[$idx]['short_style_name']= add_style($goods[$idx]['short_name'],$row['goods_name_style']);
  32. $goods[$idx]['market_price'] = price_format($row['market_price']);
  33. $goods[$idx]['shop_price']= price_format($row['shop_price']);
  34. $goods[$idx]['thumb']= get_image_path($row['goods_id'], $row['goods_thumb'], true);
  35. $goods[$idx]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
  36. $goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
  37. if (in_array($row['goods_id'], $type_array['best']))
  38. {
  39. $type_goods['best'][] = $goods[$idx];
  40. }
  41. if (in_array($row['goods_id'], $type_array['new']))
  42. {
  43. $type_goods['new'][] = $goods[$idx];
  44. }
  45. if (in_array($row['goods_id'], $type_array['hot']))
  46. {
  47. $type_goods['hot'][] = $goods[$idx];
  48. }
  49. }
复制代码



改成
  1. //取出所有符合条件的商品数据,并将结果存入对应的推荐类型数组中
  2. $sql = 'SELECT g.goods_id, cat_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
  3. "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
  4. "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img, RAND() AS rnd " .
  5. 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
  6. "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
  7. "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ";
  8. $type_merge = array_merge($type_array['new'], $type_array['best'], $type_array['hot']);
  9. $type_merge = array_unique($type_merge);
  10. $sql .= ' WHERE g.goods_id ' . db_create_in($type_merge);
  11. $sql .= ' ORDER BY g.sort_order, g.last_update DESC';

  12. $result = $GLOBALS['db']->getAll($sql);
  13. foreach ($result AS $idx => $row)
  14. {
  15. if ($row['promote_price'] > 0)
  16. {
  17. $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
  18. $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
  19. }
  20. else
  21. {
  22. $goods[$idx]['promote_price'] = '';
  23. }

  24. $goods[$idx]['id']= $row['goods_id'];$goods[$idx]['cat_id'] = $row['cat_id'];
  25. $goods[$idx]['name']= $row['goods_name'];
  26. $goods[$idx]['brief']= $row['goods_brief'];
  27. $goods[$idx]['brand_name']= isset($goods_data['brand'][$row['goods_id']]) ? $goods_data['brand'][$row['goods_id']] : '';
  28. $goods[$idx]['goods_style_name']= add_style($row['goods_name'],$row['goods_name_style']);

  29. $goods[$idx]['short_name']= $GLOBALS['_CFG']['goods_name_length'] > 0 ?
  30. sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
  31. $goods[$idx]['short_style_name']= add_style($goods[$idx]['short_name'],$row['goods_name_style']);
  32. $goods[$idx]['market_price'] = price_format($row['market_price']);
  33. $goods[$idx]['shop_price']= price_format($row['shop_price']);
  34. $goods[$idx]['thumb']= get_image_path($row['goods_id'], $row['goods_thumb'], true);
  35. $goods[$idx]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
  36. $goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
  37. if (in_array($row['goods_id'], $type_array['best']))
  38. {
  39. $type_goods['best'][] = $goods[$idx];
  40. }
  41. if (in_array($row['goods_id'], $type_array['new']))
  42. {
  43. $type_goods['new'][] = $goods[$idx];
  44. }
  45. if (in_array($row['goods_id'], $type_array['hot']))
  46. {
  47. $type_goods['hot'][] = $goods[$idx];
  48. }
  49. }
复制代码



把你原來的recommend_best.lbi內容

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  2. <!-- {if $best_goods} -->

  3. <!--{foreach from=$categories item=cat}-->
  4. <div class="box box_1">

  5. <a href="../search.php?intro=best" >精品推荐</a>

  6. <div class="cat_tit"> <span><a href="{$cat.url}">{$cat.name|escape:html}</a></span> <ahref="../search.php?intro=best">更多>></a></div>
  7. <div class="goods_cat">
  8. <div class="clearfix goodsBox" >

  9. <!--{foreach from=$cat_list item=goods}-->这一句该如何写才能遍历对应的商品

  10. <div class="goodsItem ">
  11. <a href="{$goods.url}"><img src="{$goods.thumb}" alt="{$goods.name|escape:html}" class="goodsimg" /></a><br />
  12. <p><a href="{$goods.url}" title="{$goods.name|escape:html}">{$goods.short_name|escape:html}</a></p>
  13. <!-- {if $goods.promote_price neq ""} -->
  14. <font class="shop_s">{$goods.promote_price}</font>
  15. <!-- {else}-->
  16. <font class="shop_s">{$goods.shop_price}</font>
  17. <!--{/if}-->
  18. </div>

  19. <!--{/foreach}-->

  20. </div>
  21. </div>
  22. </div>
  23. <div class="blank"></div>
  24. <!--{/foreach}-->
  25. <!-- {/if} -->
复制代码




全部改為
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  2. <!-- {if $best_goods} -->

  3. <!--{foreach from=$categories item=cat}-->
  4. <div class="box box_1">

  5. <a href="../search.php?intro=best" >精品推荐</a>

  6. <div class="cat_tit"> <span><a href="{$cat.url}">{$cat.name|escape:html}</a></span> <a href="../search.php?intro=best">更多>></a> </div>
  7. <div class="goods_cat ">
  8. <div class="clearfix goodsBox " >

  9. <!--{foreach from=$best_goods item=goods}-->
  10. <!--{if $cat.id eq$goods.cat_id}-->
  11. <div class="goodsItem ">
  12. <a href="{$goods.url}"><img src="{$goods.thumb}" alt="{$goods.name|escape:html}" class="goodsimg" /></a><br />
  13. <p><a href="{$goods.url}" title="{$goods.name|escape:html}">{$goods.short_name|escape:html}</a></p>
  14. <!-- {if $goods.promote_price neq ""} -->
  15. <font class="shop_s">{$goods.promote_price}</font>
  16. <!-- {else}-->
  17. <font class="shop_s">{$goods.shop_price}</font>
  18. <!--{/if}-->
  19. </div>
  20. <!--{/if}-->
  21. <!--{/foreach}-->

  22. </div>
  23. </div>
  24. </div>
  25. <div class="blank"></div>
  26. <!--{/foreach}-->
  27. <!-- {/if} -->
复制代码


其他的文件都动没动?

針對精品推薦,只需要改這2個檔案

lib_goods.php,阅读一下,仿照现有的代码写!

ECSHOP学习资料:http://www.phpally.com

原理只有一个
通过parent_id取得对应的cat_id
循环cat_id取得goods表里面的cat_id对应的商品