1. 程式人生 > >鏈接怎麽設置點擊一次..數據庫點擊量加1

鏈接怎麽設置點擊一次..數據庫點擊量加1

get href ati += bsp save 參數 this 一次

點擊一個鏈接時要將數據庫中的相對應的訪問數量+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‘);
}

鏈接怎麽設置點擊一次..數據庫點擊量加1