链接怎么设置点击一次..数据库点击量加1

点击一个链接时要将数据库中的相对应的访问数量+1的话,只能在当前页面写一个方法用js去访问

通过js获取要点击的链接的参数 用ajax将参数传到控制器 ,在控制器中获取传过来的参数

查找数据库中是否有记录,若有可以将此条数据获取之后再对应的字段+1

<a href='<?=Url::to(['aaa', 'id' => 1])?>' class = 'count_add'>点击</a>

<script>
$(funcation() {
  $(document).on('click', '.count_add', function () {
    var id = $(this).attr('href');
    id=id.substr(id.indexOf("=")+1,id.length-1);
    $.get('<?=Url::to(['countadd']);?>', {'id':id}, function () {
    });
  });
});
</script>

controller:

public function actionCountadd()
{
  $id = intval($_GET['id']);
  $bucket = Bucket::find()->andWhere(['user_id' => $this->_userId, 'id' => $id])->one();
  if($bucket) {
    $bucket->count += 1;
    $bucket->save();
  }
  return $this->redirect('index');
}

原文地址:https://www.cnblogs.com/l-zl/p/7117238.html