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

发布于 2024-08-25 11:44:53 字数 218 浏览 4 评论 0原文

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

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

我对 do 进行了快速搜索,似乎没有任何模块立即脱颖而出。有人有这方面的经验吗?

I currently use the statistics module (core) on my Drupal 6 install. This increments a count in the {node_counter} table every time the node is viewed, and this works.

My question is - can I programmatically increment this counter as well? I am looking to achieve this when users interact with content created from views (say click a lightbox), so being able to update the table with AJAX would be ideal.

I have done a quick search on d.o and there doesn't appear to be any modules that stick out straight away. Does anyone have any experience with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-09-01 11:44:53

为此制作一个自定义模块应该不难。

统计模块运行的查询是:

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 事件,但这也不应该太难。

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

It shouldn't be hard to make a custom module for this.

The query that the statistics module runs is:

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

The only thing we need to do, is to replace arg(1) with the node id we want to add a count to this could be done in a custom module something like this.

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

All that's left is to implement a custom access control function, you can check if the request is ajax or make whatever control you like, the function must just return TRUE or FALSE. You also need to make a ajax event with the node id in your setting, but that shouldn't be too hard either.

You need to hit the url custom/ajax/2 to update node with id 2 etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文