PHPcms貌似只有在展示show页面才能获取文章点击数,如何让他在首页和列表页也显示文章点击数,请往下面看
静态页显示点击数量,PHP里面点击数量在首页无法显示,这面这个方法就能实现
{pc:content action="lists" catid="$catid" num="15" order="id DESC" page="$page"}
<ul>
<li style="background:#1170b4;"><a style="background:none; color:#FFF;" class="center ah">标题</a><span style="color:#FFF;">点击次数</span><span style="color:#FFF;">发布时间</span></li>
{loop $data $r}
<li><a href="{$r[url]}" target="_blank"{title_style($r[style])}>{$r[title]}</a><span id="hits{$r[id]}">0</span><span>{date('Y-m-d',$r[inputtime])}</span></li>
{/loop}
</ul>
<div id="pages" class="text-c">{$pages}</div>
{loop $data $r}
{if $n!=1}
<?PHP
$d=",";
?>
{/if}
<?PHP
$ids.=$d.$r[id];
?>
{/loop}
<script language="JavaScript" src="{APP_PATH}api.PHP?op=count_list&ids={$ids}&modelid={$modelid}"></script>
{/pc}
新建一个count_list.PHP,内容如下,上传到api目录中
<?PHP
defined('IN_PHPCMS') or exit('No permission resources.');
/**
* 点击统计
*/
$db = '';
$db = pc_base::load_model('hits_model');
$ids=$_GET['ids'];
$data=explode(",",$ids);
if($_GET['modelid']) {
foreach($data as $id){
$model_arr = array();
$model_arr = getcache('model','commons');
$modelid = intval($_GET['modelid']);
$hitsid = 'c-'.$modelid.'-'.$id;
$r = get_count($hitsid);
if(!$r) exit;
extract($r);
echo "\$('#hits".$id."').html('".$views."');\n\r";
}
}
/**
* 获取点击数量
* @param $hitsid
*/
function get_count($hitsid) {
global $db;
$r = $db->get_one(array('hitsid'=>$hitsid));
if(!$r) return 0;
return $r;
}
?>
应某朋友的要求,下面放出在首页调用点击量的代码:
这部分是调用标签的代码,在首页每用一次,就复制一次过去。看懂了的话可以随便改,注意hits-1-{$r[id]}这个就是了~
{pc:content action="lists" catid="1" num="15" order="id DESC"}
<?PHP
$ids="";
?>
<ul>
<li style="background:#1170b4;"><a style="background:none; color:#FFF;" class="center ah">标题</a><span style="color:#FFF;">点击次数</span><span style="color:#FFF;">发布时间</span></li>
{loop $data $r}
<li><a href="{$r[url]}" target="_blank"{title_style($r[style])}>{$r[title]}</a><span id="hits-1-{$r[id]}">0</span><span>{date('Y-m-d',$r[inputtime])}</span></li>
{/loop}
</ul>
{loop $data $r}
{if $n!=1}
<?PHP
$d=",";
?>
{/if}
<?PHP
$ids.=$d.$r[id];
?>
{/loop}
<script language="JavaScript" src="{APP_PATH}api.PHP?op=count_index&ids={$ids}&modelid=1"></script>
{/pc}
新建一个count_index.PHP,内容如下,上传到api目录中
<?PHP
defined('IN_PHPCMS') or exit('No permission resources.');
/**
* 点击统计
*/
$db = '';
$db = pc_base::load_model('hits_model');
$ids=$_GET['ids'];
$data=explode(",'commons');
$modelid = intval($_GET['modelid']);
$hitsid = 'c-'.$modelid.'-'.$id;
$r = get_count($hitsid);
if(!$r) exit;
extract($r);
echo "\$('#hits-".$modelid."-".$id."').html('".$views."');\n\r";
}
}
/**
* 获取点击数量
* @param $hitsid
*/
function get_count($hitsid) {
global $db;
$r = $db->get_one(array('hitsid'=>$hitsid));
if(!$r) return 0;
return $r;
}
?>
红色的那个1换成你实际的模型ID
比如,你的标签是调用内容模型的内容,就填写内容模型的ID