首页调用商品分类的文章,如何修改为“按照最后更新时间,降序”排列

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



我在网上搜集到了几点。做了如下修改,效果不错。
includes/lib.goods.php


  1. function index_get_cat_id_goods_best_list($cat_id = '', $num = '')
  2. {
  3. $sql = 'Select g.goods_id, g.cat_id,c.parent_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
  4. "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
  5. "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, " .
  6. "g.is_best, g.is_new, g.is_hot, g.is_promote " .
  7. 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
  8. 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS c ON c.cat_id = g.cat_id ' .
  9. "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
  10. "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
  11. "Where g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".
  12. $sql .= " AND (c.parent_id =" . $cat_id. " OR g.cat_id = " . $cat_id ." OR g.cat_id ". db_create_in(array_unique(array_merge(array($cat_id), array_keys(cat_list($cat_id, 0, false))))) .")";
  13. $sql .= " LIMIT $num";
  14. $res = $GLOBALS['db']->getAll($sql);
  15. $goods = array();
  16. foreach ($res AS $idx => $row)
  17. {
  18. $goods[$idx]['id'] = $row['article_id'];
  19. $goods[$idx]['id'] = $row['goods_id'];
  20. $goods[$idx]['name'] = $row['goods_name'];
  21. $goods[$idx]['brief'] = $row['goods_brief'];
  22. $goods[$idx]['brand_name'] = $row['brand_name'];
  23. $goods[$idx]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
  24. $goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
  25. sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
  26. $goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
  27. $goods[$idx]['market_price'] = price_format($row['market_price']);
  28. $goods[$idx]['shop_price'] = price_format($row['shop_price']);
  29. $goods[$idx]['thumb'] = empty($row['goods_thumb']) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_thumb'];
  30. $goods[$idx]['goods_img'] = empty($row['goods_img']) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_img'];
  31. $goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
  32. }
  33. return $goods;
  34. }
复制代码


首页index.php 添加了几条
  1. $smarty->assign('cat_id123_best_goods', index_get_cat_id_goods_best_list(123,4)); // 这里是哥们自己添加的
  2. $smarty->assign('cat_id124_best_goods', index_get_cat_id_goods_best_list(124,4)); // 这里是哥们自己添加的
  3. $smarty->assign('cat_id125_best_goods', index_get_cat_id_goods_best_list(125,4)); // 这里是哥们自己添加的
  4. $smarty->assign('cat_id134_best_goods', index_get_cat_id_goods_best_list(134,4)); // 这里是哥们自己添加的
复制代码

模版文件做了如下类似修改

  1. <!--{foreach from=$cat_id123_best_goods item=goods}-->
  2. <DIV class="left">
  3. <DIV class="gmg" id="four_017"><A
  4. href="{$goods.url}" title="{$goods.name|escape:html}" target="_blank" class="gn mbt10">{$goods.short_style_name}</A><A href="{$goods.url}" target="_blank"
  5. class="gi"><IMG
  6. src="{$goods.thumb}" alt="{$goods.name|escape:html}" width="179" height="145"></A>
  7. <DIV class="gp1"><SPAN class="shop_price"><B>{$goods.shop_price}</B></SPAN><SPAN class="buy"><A
  8. href="{$goods.url}" target="_blank">立即购买</A></SPAN>
  9. <DIV class="clear"></DIV></DIV>
  10. <DIV class="gp"><SPAN class="shop_price">商场价<B>{$goods.market_price}</B> </SPAN>
  11. </DIV></DIV></DIV>
  12. <!-- { /foreach }-->
复制代码



效果很好。可是问题来了,排序是从上架日期排序的。如何修改为“按照最后更新时间,降序”排列呢?坐等答案!


世道变了啊。我C......

哥们去淘宝找人花了20元搞定了。你妹妹 我也看不懂那个哥们加了啥东西。我全部发上来,大家复制到lib_goods.php 即可实现自定义商品降序排列!!!
  1. //这段是自己加的哦
  2. function index_get_cat_id_goods_best_list($cat_id = '', $num = '')
  3. {
  4. $sql = 'Select g.goods_id, g.cat_id,c.parent_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
  5. "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
  6. "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, " .
  7. "g.is_best, g.is_new, g.is_hot, g.is_promote " .
  8. 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
  9. 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS c ON c.cat_id = g.cat_id ' .
  10. "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
  11. "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
  12. "Where g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".
  13. $sql .= " AND (c.parent_id =" . $cat_id. " OR g.cat_id = " . $cat_id ." OR g.cat_id ". db_create_in(array_unique(array_merge(array($cat_id), array_keys(cat_list($cat_id, 0, false))))) .")";
  14. $sql .= " ORDER BY g.goods_id DESC LIMIT $num";
  15. $res = $GLOBALS['db']->getAll($sql);
  16. $goods = array();
  17. foreach ($res AS $idx => $row)
  18. {
  19. $goods[$idx]['id'] = $row['article_id'];
  20. $goods[$idx]['id'] = $row['goods_id'];
  21. $goods[$idx]['name'] = $row['goods_name'];
  22. $goods[$idx]['brief'] = $row['goods_brief'];
  23. $goods[$idx]['brand_name'] = $row['brand_name'];
  24. $goods[$idx]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
  25. $goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
  26. sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
  27. $goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
  28. $goods[$idx]['market_price'] = price_format($row['market_price']);
  29. $goods[$idx]['shop_price'] = price_format($row['shop_price']);
  30. $goods[$idx]['thumb'] = empty($row['goods_thumb']) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_thumb'];
  31. $goods[$idx]['goods_img'] = empty($row['goods_img']) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_img'];
  32. $goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
  33. }
  34. return $goods;
  35. }
复制代码

回答:
坐等高手出来!!!!

last_update 按这个字段排序



大哥,这就话加在哪里啊。不胜感激啦

last_update 按这个字段排序
netaxcess 发表于 2013-6-21 09:40

不知道加到什么地方,老大请明示!

$sql .= " ORDER BY g.goods_id DESC LIMIT $num";