【问题标题】:Update {node_counter} table programmatically in drupal在 drupal 中以编程方式更新 {node_counter} 表
【发布时间】:2010-03-20 06:58:14
【问题描述】:

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

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

我在 d.o 上进行了快速搜索,似乎没有任何模块可以立即突出显示。有人有这方面的经验吗?

【问题讨论】:

    标签: ajax drupal drupal-6 drupal-views drupal-modules


    【解决方案1】:

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

    统计模块运行的查询是:

    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 等的节点。

    【讨论】:

    • 非常感谢您的详细回答。将在自定义模块上破解。
    • 我的模块现在可以在 d.o drupal.org/project/statistics_ajax 上下载 - 以防其他人需要类似的功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    相关资源
    最近更新 更多