phpcms v9文章点击量相关汇总

1.模板中启用文章点击量统计

  1. 显示点击量: id="hits">
  2. 引入jquery库和:
  3. language="JavaScript" src="{APP_PATH}api.php?op=count&id={$id}&modelid={$modelid}">

2.让文章点击数随机增加

文件位置\api\count.php

  1. $views = $r['views'] + 1;
  2. $yesterdayviews = (date('Ymd', $r['updatetime']) == date('Ymd', strtotime('-1 day'))) ? $r['dayviews'] : $r['yesterdayviews'];
  3. $dayviews = (date('Ymd', $r['updatetime']) == date('Ymd', SYS_TIME)) ? ($r['dayviews'] + 1) : 1;
  4. $weekviews = (date('YW', $r['updatetime']) == date('YW', SYS_TIME)) ? ($r['weekviews'] + 1) : 1;
  5. $monthviews = (date('Ym', $r['updatetime']) == date('Ym', SYS_TIME)) ? ($r['monthviews'] + 1) : 1;

改为:

  1. $num = rand(1,50);
  2. $views = $r['views'] + $num;
  3. $yesterdayviews = (date('Ymd', $r['updatetime']) == date('Ymd', strtotime('-1 day'))) ? $r['dayviews'] : $r['yesterdayviews'];
  4. $dayviews = (date('Ymd', $r['updatetime']) == date('Ymd', SYS_TIME)) ? ($r['dayviews'] + $num) : $num;
  5. $weekviews = (date('YW', $r['updatetime']) == date('YW', SYS_TIME)) ? ($r['weekviews'] + $num) : $num;
  6. $monthviews = (date('Ym', $r['updatetime']) == date('Ym', SYS_TIME)) ? ($r['monthviews'] + $num) : $num;

每次随机增加1到50点击数。

3.某分类点击量总排行

  1. {pc:content action="hits" catid="$catid" num="8" order="views DESC" cache="3600"}
    • {loop $data $r}
    • href="{$r[url]}">{$r[title]}
    • {/loop}
  2. {/pc}

4.某分类点击量本月排行

  1. {pc:content action="hits" catid="$catid" num="8" order="monthviews DESC" cache="3600"}
    • {loop $data $r}
    • {number_format($r[monthviews])} href="{$r[url]}"{title_style($r[style])} class="title" title="{$r[title]}">{str_cut($r[title],56,'...')}
    • {/loop}
  2. {/pc}

5.全站热门文章排行

phpcms分多站点多模型多分类,默认是不能调取全站热门文章的,下面这种方法可调取某个站点的所有模型及所有分类中文章的点击量排行;需要在modules中自定义函数,打开phpcms\modules\content\classes\content_tag.class.php,添加一个函数:

  1. /**
  2. * sitehits站点点击排行
  3. * @param $data
  4. */
  5. public function sitehits($data){
  6. if(empty($data['siteid'])) return false;
  7. $siteid = intval($data['siteid']);
  8. $this->hits_db = pc_base::load_model('hits_model');
  9. $category_content = getcache('category_content','commons');
  10. $catid = '';
  11. //获取站点下所有栏目ID
  12. foreach($category_content as $key=>$val){
  13. if($val==$siteid){
  14. $catid .= $comma.$key;
  15. $comma=',';
  16. }
  17. }
  18. //获取点击排行
  19. $r = $this->hits_db->select('catid in('.$catid.')','hitsid',$data['limit'],$data['order']);
  20. $return = array();
  21. $sitemodel_model_db = pc_base::load_model('sitemodel_model');
  22. $this->db_config = pc_base::load_config('database');
  23. $tablepre = $this->db_config['default']['tablepre'];
  24. foreach($r as $key){
  25. preg_match_all('/-(\d+)-/',$key['hitsid'],$modelid);
  26. $id = substr($key['hitsid'],(strpos($key['hitsid'],'-',2)+1));
  27. $tablename = $sitemodel_model_db->get_one(array('modelid'=>$modelid[1][0]),'tablename');
  28. $this->db->table_name = $tablepre.$tablename['tablename'];
  29. $return[] = array_merge($return,$this->db->get_one(array('id'=>$id)));
  30. }
  31. return $return;
  32. }

调用方法:

 
  
  1. {pc:content action="sitehits" siteid="1" num="10" order="views DESC" cache="3600"}
  2. {loop $data $r}
  3. href="{$r[url]}" target="_blank">{$r[title]}
  4. {/loop}
  5. {/pc}

如果未达到预期效果,不妨后台更新一下缓存试试。

你可能感兴趣的:(phpcms v9文章点击量相关汇总)