以drupal编程方式更新{node_counter}表

wii*_*ifm 6 ajax drupal drupal-views drupal-6 drupal-modules

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

我的问题是 - 我可以通过编程方式递增此计数器吗?我希望当用户与从视图创建的内容(例如点击灯箱)进行交互时实现这一点,因此能够使用AJAX更新表格将是理想的选择.

我已经做了快速搜索,并且似乎没有任何模块可以立即发布.有人对这个有经验么?

goo*_*orp 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());
}
Run Code Online (Sandbox Code Playgroud)

我们唯一需要做的就是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());
   }
 }
Run Code Online (Sandbox Code Playgroud)

剩下的就是实现一个自定义访问控制功能,你可以检查请求是ajax还是做你喜欢的控制,函数必须返回TRUE或FALSE.您还需要在设置中使用节点ID创建ajax事件,但这也不应该太难.

你需要点击url custom/ajax/2来更新id为2的节点.