2013-03-31 116 views
0

我目前已經實現了一個點擊計數器,如果該值設置爲空,並且您點擊它將更新爲一個的鏈接。但是當它第二次點擊時,它不會更新它保持爲一個。如何使點擊計數器在點擊時多次更新?

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 
} 
+1

爲什麼當你在1之後立即加1時設置'$ click = 0'? –

+1

因爲您從不使用'$ set =「click = click + 1」;'但總是'$ click = 0 + 1;'。 – dfsq

+0

因爲我一直收到未分配變量的錯誤消息 – philp

回答

0
$click=0; 
$click=$click+$i; 

卸下第一線和存儲的值$單擊以會話變量或將其存儲在數據庫中。

0

在您的功能中,您應該首先檢查pages表中是否存在clicks的值(where id=$id)。如果沒有,那麼你可以給clicks=1,否則得到的是價值,並添加1

但在這裏就是我會做它:我想通過禁止在clicks列允許空編輯表格,使默認情況下,當創建新頁面時,它的默認值爲。然後我使用以下代碼:

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 
+0

非常感謝您現在的計數器工作。 – philp

0

每次調用函數時,$ click的值都設置爲0,然後在下一個語句中設置爲1。如果您希望該值持續存在,則應在表中使用一個屬性並在每次單擊時增加該值。