phpcms在任意地方调用文章点击数和评论数量

[复制链接]
defiler 发表于 2023-10-23 07:33:42|来自:北京 | 显示全部楼层 |阅读模式
phpcms原来的文章点击数量只能在show.html页面调用,并且一次性只能调取一个文章的点击数量,这在实际生产环境中,是极其不方便的,今天刚好在做二次开发的时候,就遇到这个问题,需要在首页和栏目页展示出文章的点击数量以及评论数量,我在官方的帮助文档没有找到答案,(可能有,只是我没有找到),于是只能自己动手了,顺便记录一下,方便自己今后查询,也有可能能够帮助到一些新手:
首先在phpcms/modules/content/funcionts/global.func.php里面创建两个函数:
/*
* 自定义函数库,用来取得文章列表页点击数量
*
* @param $catid 栏目CATID
* @param $id 文章ID
* @param $siteid 站点ID
* @return string
*/
function gETHits($catid,$id,$key='views',$siteid = 1){
    if(!isset($catid) || empty($catid) || !isset($id) || empty($id)) return false;
    $db = pc_base::load_model('hits_model');
    $CATEGORYS = getcache('category_content_'.$siteid,'commons');
    $modelid = $CATEGORYS[$catid]['modelid'];
    $rs = $db->get_one(array('hitsid'=>'c-'.$modelid.'-'.$id));
    return $rs[$key];
}

/*自定义函数库,用来获取文章评论数
*
* @param $siteid 站点id
* @param $catid 栏目id
* @param $id 文章id
* @return string
* */
function getComment($catid,$id,$siteid=1){
    if(!isset($catid) || empty($catid) || !isset($id) || empty($id)) return false;
    $CATEGORYS = getcache('category_content_'.$siteid,'commons');
    $modelid = $CATEGORYS[$catid]['modelid'];
    $comment_tag = pc_base::load_app_class("comment_tag", "comment");
    $comment_total = $comment_tag->count(array('commentid'=>'content_'.$catid.'-'.$id.'-'.$modelid));
    if($comment_total){
        return $comment_total;
    }else{
        return '0';
    }
}
这样,如果你在任何地方调用都可以的,但是需要在调用的页面引用自定义函数库:
{php pc_base::load_app_func('global')} 上面的getHits函数有一个弊端,如果页面访问量过大,会过多的消耗服务器的资源,我在实际中其实使用了Memcache来进行缓存,这样的话,就能减轻MySql的压力。
全部回复0 显示全部楼层
暂无回复,精彩从你开始!

快速回帖

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则