返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >Phpcms V9 调用全站文章排行的解决方法
  • 448
分享到

Phpcms V9 调用全站文章排行的解决方法

全站文章排行 2022-06-12 01:06:50 448人浏览 八月长安
摘要

默认情况下,PHPcms只支持调用当前文章排行,代码如下: {pc:content action="hits" catid="$catid" num="10"

默认情况下,PHPcms只支持调用当前文章排行,代码如下:

{pc:content action="hits" catid="$catid" num="10" order="views DESC" cache="3600"}

其中$catid为待调用栏目的id,如果想实现全站调用,需要修改phpcms\modules\content\classes\content_tag.class.php文件,找到以下函数:


复制代码代码如下:

public function hits($data) {

$catid = intval($data['catid']);

if(!$this->set_modelid($catid)) return false;

$this->hits_db = pc_base::load_model('hits_model');

$sql = $desc = $ids = '';

$array = $ids_array = array();

$order = $data['order'];

$hitsid = 'c-'.$this->modelid.'-%';

$sql = "hitsid LIKE '$hitsid'";

if(isset($data['day'])) {

$updatetime = SYS_TIME-intval($data['day'])*86400;

$sql .= " AND updatetime>'$updatetime'";

}

if($this->cateGory[$catid]['child']) {

$catids_str = $this->category[$catid]['arrchildid'];

$pos = strpos($catids_str,',')+1;

$catids_str = substr($catids_str, $pos);

$sql .= " AND catid IN ($catids_str)";

} else {

$sql .= " AND catid='$catid'";

}

$hits = array();

$result = $this->hits_db->select($sql, '*', $data['limit'], $order);

foreach ($result as $r) {

$pos = strpos($r['hitsid'],'-',2) + 1;

$ids_array[] = $id = substr($r['hitsid'],$pos);

$hits[$id] = $r;

}

$ids = implode(',', $ids_array);

if($ids) {

$sql = "status=99 AND id IN ($ids)";

} else {

$sql = '';

}

$this->db->table_name = $this->tablename;

$result = $this->db->select($sql, '*', $data['limit'],'','','id');

foreach ($ids_array as $id) {

if($result[$id]['title']!='') {

$array[$id] = $result[$id];

$array[$id] = array_merge($array[$id], $hits[$id]);

}

}

return $array;

}

  修改代码(见注释):


复制代码代码如下:

public function hits($data) {

$catid = intval($data['catid']);

$this->hits_db = pc_base::load_model('hits_model');

$sql = $desc = $ids = '';

$array = $ids_array = array();

$order = $data['order'];

$hitsid = 'c-'.$this->modelid.'-%';

$sql = "hitsid LIKE '$hitsid'";

if(isset($data['day'])) {

$updatetime = SYS_TIME-intval($data['day'])*86400;

$sql .= " AND updatetime>'$updatetime'";

}

if(!empty($catid) && $catid>0) { //添加判断:id是否为空

if(!$this->set_modelid($catid)) return false;

if($this->category[$catid]['child']) {

$catids_str = $this->category[$catid]['arrchildid'];

$pos = strpos($catids_str,',')+1;

$catids_str = substr($catids_str, $pos);

$sql .= " AND catid IN ($catids_str)";

} else {

$sql .= " AND catid='$catid'";

}

}

$hits = array();

$result = $this->hits_db->select($sql, '*', $data['limit'], $order);

foreach ($result as $r) {

$pos = strpos($r['hitsid'],'-',2) + 1;

$ids_array[] = $id = substr($r['hitsid'],$pos);

$hits[$id] = $r;

}

$ids = implode(',', $ids_array);

if($ids) {

$sql = "status=99 AND id IN ($ids)";

} else {

$sql = '';

}

$this->db->table_name = $this->tablename;

$result = $this->db->select($sql, '*', $data['limit'],'','','id');

foreach ($ids_array as $id) {

if($result[$id]['title']!='') {

$array[$id] = $result[$id];

$array[$id] = array_merge($array[$id], $hits[$id]);

}

}

return $array;

}

修改代码后,无论设置栏目id为0或空,都能调取全站文章排行。

调用方法1:

{pc:content action="hits" catid="0" num="10" order="views DESC" cache="3600"}

调用方法2:

{pc:content action="hits" num="10" order="views DESC" cache="3600"}

--结束END--

本文标题: Phpcms V9 调用全站文章排行的解决方法

本文链接: https://lsjlt.com/news/31702.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作