通过js ajax增加文章访问量
- 为文章模型增加ajax提交页面,方法名为articleViewAjax
- 文章页提交当前文章id,示例代码在 article_content.php 内
- 接收ajax提交内容,示例代码在example_ajaxarticleview.php 内
js 代码示例
<?php
$url=U($cid,'ajax');//通过U函数获取当前栏目标识为ajax的访问地址
?>
<script>
function addView(id) {
fetch('{$url}', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'id=' + encodeURIComponent(id)
});
}
addView({$id});//id为当前文章id
</script>
articleViewAjax代码示例
function articleViewAjax($channel){
$article=C('cms:article:getOne',array('cid'=>$channel['id'],'where.id'=>@$_POST['id']));//查询对应的文章
if($article){
C('cms:article:edit',array('cid'=>$channel['id'],'id'=>$article['id'],'viewcount'=>$article['viewcount']+1));//访问量+1
return true;
}
}
