phpcms文章点击量相关汇总——phpcms文章统计功能定制开发
Author:zhoulujun Date:
1.模板中启用文章点击量统计
在 phpcms/templates/default/content/show.html,修改模板
引入jquery库:
<script language="JavaScript" src="{APP_PATH}api.php?op=count&id={$id}&modelid={$modelid}"></script>
显示点击量:
<span id="hits"></span>
我的头部修改为:
{template 'content', 'header'} <article class="main-content wrap" role="main"> <div class="crumbs ov"> <a href="https://www.zhoulujun.cn">home</a><span> > </span>{catpos($catid)} </div> <article id="Article" class="article col-left fl" role="content"> <header> <h1 class="main-title txth" aria-level="1">{$title}</h1> <p class="detail-info tc"> {if !empty($username)}<strong>Author:</strong><span>{$username}</span> {/if} <strong>Date:</strong><time>{substr($inputtime, 0,10)}</time> <label class="hidden"> <strong>Hits:</strong><span id="hits" ></span ></label> </p> </header> {if $description} <summary class="summary">{$description}</summary> {/if} <article class="con-box" role="article"> {if $allow_visitor==1} {$content}
相应的api/count.php代码修改为:
<?php defined('IN_PHPCMS') or exit('No permission resources.'); /** * 点击统计 */ $db = ''; $db = pc_base::load_model('hits_model'); header ("Content-type: text/javascript"); if($_GET['modelid'] && $_GET['id']) { $model_arr = array(); $model_arr = getcache('model','commons'); $modelid = intval($_GET['modelid']); $hitsid = 'c-'.$modelid.'-'.intval($_GET['id']); $r = get_count($hitsid); if(!$r) exit; extract($r); hits($hitsid); echo "\$('#todaydowns').html('$dayviews');"; echo "\$('#weekdowns').html('$weekviews');"; echo "\$('#monthdowns').html('$monthviews');"; } elseif($_GET['module'] && $_GET['id']) { $module = $_GET['module']; if((preg_match('/([^a-z0-9_\-]+)/i',$module))) exit('1'); $hitsid = $module.'-'.intval($_GET['id']); $r = get_count($hitsid); if(!$r) exit; extract($r); hits($hitsid); } /** * 获取点击数量 * @param $hitsid */ function get_count($hitsid) { global $db; $r = $db->get_one(array('hitsid'=>$hitsid)); if(!$r) return false; return $r; } /** * 点击次数统计 * @param $contentid */ function hits($hitsid) { global $db; $r = $db->get_one(array('hitsid'=>$hitsid)); if(!$r) return false; $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; $sql = array('views'=>$views,'yesterdayviews'=>$yesterdayviews,'dayviews'=>$dayviews,'weekviews'=>$weekviews,'monthviews'=>$monthviews,'updatetime'=>SYS_TIME); return $db->update($sql, array('hitsid'=>$hitsid)); } ?> $('#hits').html('<?php echo $views?>').parent().removeClass("hidden");
这样就好了
2.让文章点击数随机增加
文件位置\api\count.php
/** * 点击次数统计 * @param $contentid */ function hits($hitsid) { global $db; $r = $db->get_one(array('hitsid'=>$hitsid)); if(!$r) return false; $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; $sql = array('views'=>$views,'yesterdayviews'=>$yesterdayviews,'dayviews'=>$dayviews,'weekviews'=>$weekviews,'monthviews'=>$monthviews,'updatetime'=>SYS_TIME); return $db->update($sql, array('hitsid'=>$hitsid)); }
改为:
$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}
如果未达到预期效果,不妨后台更新一下缓存试试。
转载本站文章《phpcms文章点击量相关汇总——phpcms文章统计功能定制开发》,
请注明出处:https://www.zhoulujun.cn/html/php/phpcms/2022_0530_8822.html