Updating the {node_counter} table programmatically in drupal

I am currently using the statistics module (kernel) for my Drupal 6 installation. This increases the score in the {node_counter} table every time a node is scanned, and it works.

My question is, can I programmatically increase this counter? I want to achieve this when users interact with content created from views (say, click the lightbox), so it would be convenient to update the table using AJAX.

I did a quick search on do and there seems to be no modules left that are sticking out right now. Does anyone have experience with this?

+6
ajax drupal drupal-views drupal-6 drupal-modules
source share
1 answer

No need to create your own module for this.

The query executed by the statistics module:

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 replace arg(1) with the node identifier that we want to add so that the count can be executed in the user module 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 remains is to implement a custom access control function, you can check if the request is ajax or make any control you like, the function should simply return TRUE or FALSE. You also need to make an ajax event with the node id in your setup, but that should not be too heavy either.

You need to click custom/ajax/2 URL to update node with id 2, etc.

+9
source share

All Articles