ECSHOP 文章排序问题 按照发布时间排序 按照文章
最近在网上看到很多朋友在咨询ECSHOP首页调用文章,但是文章按照发布时间递增排序的解决方法,今天和大家分享一下我们的解决方法.
首页要调用制定栏目的文章,相信问这个问题的朋友一定对下边的代码不会陌生:
- function index_get_class_articles($cat_aid, $cat_num)
- {
- $sql = "Select article_id, title,open_type,cat_id,file_url FROM " .$GLOBALS['ecs']->table('article'). " Where cat_id = ".$cat_aid." and is_open = 1 LIMIT " . $cat_num;
- $res = $GLOBALS['db']->getAll($sql);
- $arr = array();
- foreach ($res AS $idx => $row)
- {
- $arr[$idx]['id'] = $row['article_id'];
- $arr[$idx]['title'] = $row['title'];
- $arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ?
- sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];
- $arr[$idx]['cat_name'] = $row['cat_name'];
- $arr[$idx]['add_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
- $arr[$idx]['url'] = $row['open_type'] != 1 ?
- build_uri('article', array('aid' => $row['article_id']), $row['title']) : trim($row['file_url']);
- $arr[$idx]['cat_url'] = build_uri('article_cat', array('acid' => $row['cat_id']));
- }
- return $arr;
- }
只要我们对里的代理进行适当的修改一下,文章排序问题就解决了,我们是按照文章ID来进行降序排列的,代码如下:
- function index_get_class_articles($cat_aid, $cat_num)
- {
- $sql = "Select article_id, title,open_type,cat_id,file_url FROM " .$GLOBALS['ecs']->table('article'). " Where cat_id = ".$cat_aid." and is_open = 1 order by article_id desc LIMIT 0," . $cat_num;
- $res = $GLOBALS['db']->getAll($sql);
- $arr = array();
- foreach ($res AS $idx => $row)
- {
- $arr[$idx]['id'] = $row['article_id'];
- $arr[$idx]['title'] = $row['title'];
- $arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ?
- sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];
- $arr[$idx]['cat_name'] = $row['cat_name'];
- $arr[$idx]['add_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
- $arr[$idx]['url'] = $row['open_type'] != 1 ?
- build_uri('article', array('aid' => $row['article_id']), $row['title']) : trim($row['file_url']);
- $arr[$idx]['cat_url'] = build_uri('article_cat', array('acid' => $row['cat_id']));
- }
- return $arr;
- }
其实很简单,很多朋友都知道了方法,但是有一点小细节没有注意到,那就是LIMIT 0," . $cat_num;很多朋友都忽视这里,因为默认的LIMIT " . $cat_num;是这样,但是当我们加上order by article_id desc这个排序的功能后,那么LIMIT后边就必须加上0,来进行限制了.