如何使单击计数器在单击时更新不止一次


How to make a click counter update more than once when clicked?

我目前已经实现了一个点击计数器,如果值被设置为null,你点击链接,它将更新为一个。但是当它第二次点击时,它不会更新,它保持为一个。

public function getUpdateClick($Id) {
    $Id=DB::escape_string($Id);
    $i=1;
    $click=0;
    $click=$click+$i;
    $updatesArray=array('clicks'=>$click);// the column which is going to be updated
    $this->setTable('pages'); //setting the table name
    $where="id=$Id";
    $result=$this->update($updatesArray,$where); // query executed
}
$click=0;
$click=$click+$i;

删除第一行并将$click的值存储为会话变量或存储在数据库中

每次调用该函数时,$click的值被设置为0,然后在下一个语句中设置为1。如果希望值持久存在,则应该在表中使用一个属性,并在每次单击时增加该属性。

在函数中,您应该首先检查特定页面(where id=$id)的pages表上是否有clicks的值。如果没有,则可以给出clicks=1,否则得到该值并加1。

但这是我怎么做的:我编辑表通过禁用允许NULL在clicks列,这样在默认情况下,当一个新的页面被创建,它的默认值是0。然后我将使用下面的代码:

public function getUpdateClick($Id){
    $Id=DB::escape_string($Id);
    //You can edit the 3 lines below to check the database 
    //  in pages table in clicks column where id=$Id in your custom way
    $fetching sql_result = mysql_query('SELECT * FROM pages where id=$Id')
    $row = mysql_fetch_assoc($fetching sql_result);
    $current_number_of_clicks = $row['clicks'];
    $click= $current_number_of_clicks;
    $click=$click+1;
    $updatesArray=array('clicks'=>$click);// the column which is going to be updated
    $this->setTable('pages'); //setting the table name
    $set="click=click+1"; // column and value
    $where="id=$Id";  // 
    $result=$this->update($updatesArray,$where); // query executed
}
} // I assume this closes your class