7

我目前在我的 Drupal 6 安装中使用统计模块(核心)。每次查看节点时,这都会增加 {node_counter} 表中的计数,这是有效的。

我的问题是 - 我也可以以编程方式增加这个计数器吗?当用户与从视图创建的内容交互时(比如单击灯箱),我希望实现这一点,因此能够使用 AJAX 更新表格将是理想的。

我对 do 进行了快速搜索,似乎没有任何模块可以立即突出。有人对这个有经验么?

4

1 回答 1

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());
}

我们唯一需要做的就是替换arg(1)为我们想要添加计数的节点 ID,这可以在类似这样的自定义模块中完成。

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。您还需要在您的设置中使用节点 ID 创建一个 ajax 事件,但这也不应该太难。

您需要点击 urlcustom/ajax/2来更新 id 为 2 等的节点。

于 2010-03-20T11:56:03.523 回答