ecshop 模仿凡客实现成列效果 foreach 或用while

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

期待高手回答

回答:
无需建表,比如你想列出最近一周的商品,然后再往后推一周,继续这样往后推的话,

首先你date出今天的日期($today),然后减去604800也就是正好7天,把这个结果保存到一个变量($days),去数据库查出<=今天且>=$days这个数字的商品,ec表中字段是add_time,并把取得的这个数组保存到一个索引自增长的数组中,比如$arr[],然后把$days的值赋给$today,再用$today继续减去 604800得出新的 $days值, 再去查找商品,最后当查询结果为空时break跳出循环。。

然后到模板中循环$arr,控制下样式就可以了。 数据多的话不知道这样循环慢不慢。。

  1. /**
  2. * 获得所有的专题活动
  3. *
  4. * @access public
  5. * @return array
  6. */
  7. function get_topic_list($size, $page){


  8. //当前时间
  9. $time = gmtime();
  10. //
  11. $sql = "SELECT min(start_time) AS min, max(start_time) as max FROM " . $GLOBALS['ecs']->table('topic'). " ORDER BY start_time DESC ";
  12. //获得当前分类下商品价格的最大值、最小值

  13. $row = $GLOBALS['db']->getRow($sql);
  14. $max = $row['max'];
  15. $min = $row['min'];

  16. $sql = "SELECT *,week(from_unixtime(start_time)) stime FROM " . $GLOBALS['ecs']->table('topic') . " WHERE start_time BETWEEN '$min' AND '$max' ORDER BY start_time DESC ";
  17. $res = $GLOBALS['db']->getAll($sql);

  18. $arr = array();

  19. foreach ($res as $key => $row)
  20. {

  21. $week = date("YW",$row['start_time']+28800);

  22. $arr[$week]['weeka'] = date("YW",$row['start_time']);
  23. $arr[$week]['this'] = date("YW");
  24. $last = mktime(0,0,0,date("m"),date("d")-date("w")+1-7,date("Y"));
  25. $arr[$week]['last'] = date("YW",$last);

  26. $weekday = date("w",$row['start_time']);
  27. $start_week = mktime(0,0,0,date("m",$row['start_time']),date("d",$row['start_time'])-$weekday+1,date("Y",$row['start_time']));
  28. $end_week = mktime(23,59,59,date("m",$row['start_time']),date("d",$row['start_time'])-$weekday+7,date("Y",$row['start_time']));
  29. $arr[$week]['start_week'] = date('Y年m月d日', $start_week);
  30. $arr[$week]['end_week'] = date('Y年m月d日', $end_week);

  31. $start_week = $start_week - 28800;
  32. $end_week = $end_week - 28800;
  33. $arr[$week]['week_count'] = $GLOBALS['db']->getOne("SELECT count(*) FROM " . $GLOBALS['ecs']->table('topic') . " WHERE start_time >= '$start_week' AND start_time <= '$end_week'");
  34. $arr[$week]['week'][$key]['id'] = $row['topic_id'];
  35. $arr[$week]['week'][$key]['title'] = $row['title'];
  36. $arr[$week]['week'][$key]['start_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['start_time']);
  37. if(!empty($row['title_pic']))
  38. {
  39. $arr[$week]['week'][$key]['title_pic'] = $row['title_pic'];
  40. }
  41. else
  42. {
  43. $arr[$week]['week'][$key]['topic_img'] = $row['topic_img'];
  44. $arr[$week]['week'][$key]['title_pic'] = (empty($row['topic_img'])) ? $GLOBALS['_CFG']['no_picture'] : $row['topic_img'];
  45. }
  46. $arr[$week]['week'][$key]['url'] = build_uri('topic', array('cid'=>$row['topic_id']), $row['title']);

  47. }

  48. return $arr;
  49. }
复制代码




我这样做已经将全部的活动都查出来了,可惜的就是在分页的时候有问题


这里注意一下的是我不是有规律的按 每月 一页, 本来想把 每周放进一个数组里面然后分页的时候直接显示 4个 数组,可是这样的话 就没有效率了,请教高手 怎么在 查询出的全部内容里 再做分页呢?????