1.模板中启用文章点击量统计
显示点击量:<span id="hits"></span> 引入jquery库和: <script language="JavaScript" src="{APP_PATH}api.php?op=count&id={$id}&modelid={$modelid}"></script>
2.让文章点击数随机增加
文件位置\api\count.php
$views = $r['views'] + 1; $yesterdayviews = (date('Ymd', $r['updatetime']) == date('Ymd', strtotime('-1 day'))) ? $r['dayviews'] : $r['yesterdayviews']; $dayviews = (date('Ymd', $r['updatetime']) == date('Ymd', SYS_TIME)) ? ($r['dayviews'] + 1) : 1; $weekviews = (date('YW', $r['updatetime']) == date('YW', SYS_TIME)) ? ($r['weekviews'] + 1) : 1; $monthviews = (date('Ym', $r['updatetime']) == date('Ym', SYS_TIME)) ? ($r['monthviews'] + 1) : 1;
改为:
$num = rand(1,50); $views = $r['views'] + $num; $yesterdayviews = (date('Ymd', $r['updatetime']) == date('Ymd', strtotime('-1 day'))) ? $r['dayviews'] : $r['yesterdayviews']; $dayviews = (date('Ymd', $r['updatetime']) == date('Ymd', SYS_TIME)) ? ($r['dayviews'] + $num) : $num; $weekviews = (date('YW', $r['updatetime']) == date('YW', SYS_TIME)) ? ($r['weekviews'] + $num) : $num; $monthviews = (date('Ym', $r['updatetime']) == date('Ym', SYS_TIME)) ? ($r['monthviews'] + $num) : $num;
每次随机增加1到50点击数。
3.某分类点击量总排行
{pc:content action="hits" catid="$catid" num="8" order="views DESC" cache="3600"} <ul> {loop $data $r} <li><a href="{$r[url]}">{$r[title]}</a></li> {/loop} </ul> {/pc}
4.某分类点击量本月排行
{pc:content action="hits" catid="$catid" num="8" order="monthviews DESC" cache="3600"} <ul> {loop $data $r} <li><span>{number_format($r[monthviews])}</span><a href="{$r[url]}"{title_style($r[style])} class="title" title="{$r[title]}">{str_cut($r[title],56,'...')}</a></li> {/loop} </ul> {/pc}
5.全站热门文章排行
phpcms分多站点多模型多分类,默认是不能调取全站热门文章的,下面这种方法可调取某个站点的所有模型及所有分类中文章的点击量排行;需要在modules中自定义函数,打开phpcms\modules\content\classes\content_tag.class.php,添加一个函数:
/** * sitehits站点点击排行 * @param $data */ public function sitehits($data){ if(empty($data['siteid'])) return false; $siteid = intval($data['siteid']); $this->hits_db = pc_base::load_model('hits_model'); $category_content = getcache('category_content','commons'); $catid = ''; //获取站点下所有栏目ID foreach($category_content as $key=>$val){ if($val==$siteid){ $catid .= $comma.$key; $comma=','; } } //获取点击排行 $r = $this->hits_db->select('catid in('.$catid.')','hitsid',$data['limit'],$data['order']); $return = array(); $sitemodel_model_db = pc_base::load_model('sitemodel_model'); $this->db_config = pc_base::load_config('database'); $tablepre = $this->db_config['default']['tablepre']; foreach($r as $key){ preg_match_all('/-(\d+)-/',$key['hitsid'],$modelid); $id = substr($key['hitsid'],(strpos($key['hitsid'],'-',2)+1)); $tablename = $sitemodel_model_db->get_one(array('modelid'=>$modelid[1][0]),'tablename'); $this->db->table_name = $tablepre.$tablename['tablename']; $return[] = array_merge($return,$this->db->get_one(array('id'=>$id))); } return $return; }
调用方法:
{pc:content action="sitehits" siteid="1" num="10" order="views DESC" cache="3600"} {loop $data $r} <li><a href="{$r[url]}" target="_blank">{$r[title]}</a></li> {/loop} {/pc}
如果未达到预期效果,不妨后台更新一下缓存试试。
有朋自远方来...评论一下呗O(∩_∩)O