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