Adding an 'auto_increment' field
Last updated on
30 April 2025
You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules
There's now a module to do this, Serial Field, I recommend you to use that one instead of this option
This snippet will create an auto increment field. You could use this if you have a content type where you need to register a transaction number and you don't want to let the users add the transaction number because you might lose the sequence.
if (!empty($node_field[0]['value'])) { // the node is not new
return $node_field[0]['value'];
}
else { // the node is new
$count = db_result(db_query("SELECT COUNT(*) from {node} WHERE type = 'transaction'"));
$node_field[0]['value'] = $count;
}
In this case the Content Type is 'Transaction' so you'll have to change that to the content type you want to count...
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion