2010-03-20 65 views
6

我目前在Drupal 6安裝中使用統計模塊(核心)。這會在每次查看節點時在{node_counter}表中增加一個計數,並且這會起作用。在drupal中以編程方式更新{node_counter}表格

我的問題是 - 我可以以編程方式增加這個計數器嗎?我希望在用戶與從視圖創建的內容交互時(例如點擊lightbox)來實現這一點,所以能夠使用AJAX更新表格將是理想的選擇。

我已經在d.o上做了一個快速搜索,並且看起來沒有任何模塊突然出現。有人對這個有經驗麼?

回答

9

爲此製作定製模塊不應該很難。

的統計模塊運行的查詢是:

db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), arg(1)); 
// If we affected 0 rows, this is the first time viewing the node. 
if (!db_affected_rows()) { 
    // We must create a new row to store counters for the new node. 
    db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES (%d, 1, 1, %d)', arg(1), time()); 
} 

我們需要做的唯一的事情,是我們要添加一個計數,這可能在定製完成節點ID替換arg(1)模塊是這樣的。

function custom_module_menu() { 
    $items['custom/ajax/%node'] = array(
    'title' => 'Update count', 
    'page callback' => 'custom_module_update_counter', 
    'page arguments' => array(2), 
    'access callback' => array('custom_module_access_control'), 
); 

function custom_module_update_counter($node) { 
    db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), $node->nid); 
    // If we affected 0 rows, this is the first time viewing the node. 
    if (!db_affected_rows()) { 
    // We must create a new row to store counters for the new node. 
    db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES (%d, 1, 1, %d)', $node->nid, time()); 
    } 
} 

所有剩下的就是實現一個自定義訪問控制功能,您可以檢查是否請求是AJAX,或讓你喜歡的任何控制,該函數必須只返回TRUE或FALSE。您還需要在設置中使用節點標識創建一個ajax事件,但這也不應該太難。

您需要點擊的URL custom/ajax/2更新ID爲2等

+0

三江源很爲你詳細解答節點。將開始在自定義模塊。 – wiifm 2010-03-20 22:20:05

+0

我的模塊現在可以在http://drupal.org/project/statistics_ajax的d.o上下載 - 以防其他人需要類似的功能 – wiifm 2010-06-12 04:35:09