I'd like nodes of a certain type to have their title field as an incrementing integer (e.g. 001, 002, 003, etc.)
I've tried using the [nid] token, but it increments even when another type of node is created, giving this sort of effect: 001, 003, 004, etc.

Anyone know an easy way of doing this?

Comments

no2e’s picture

(Subscribe)

I just created a forum topic about that topic, before I read this issue: http://drupal.org/node/305328

Anonymous’s picture

I found one way to do it...

I put the following code in the pattern for my auto nodetitle (set to PHP obviously):

<?php
$sql = "SELECT count(nid) AS number FROM {node} WHERE type = 'node_type'";
$result = db_query($sql);
$data = db_fetch_array($result);
$id = sprintf("%03d", $data['number'] + 1);

return $id;
?>

It counts the number of nodes of a given type (node_type) and adds some leading 0's to it.

The only problem is that it doesn't count deleted nodes (i.e. if you have 15 nodes (the last of which has a title of '015') and you delete a node, the next one you create will be given the title of '015'...) This may or may not be ideal for certain situations, in my case I'll just stop users from deleting nodes.

Hope this helps!

EDIT: Oops, changed the sprintf function to increment the number so if there are 15 existing nodes, the new one will be titled '016' :)

Anonymous’s picture

Just came across another problem...

When you edit an existing node, the PHP is run again and the number changes.
Any ideas how to stop this happening?

asak’s picture

could this be the answer?

Actually, seems that this is the answer...

Anonymous’s picture

I actually just ended up using the Workflow-NG module to run the PHP code whenever a node of a particular type was created. This stops it running after being edited.

asak’s picture

BWPanda: My problem is that nodes on my site are often deleted.... and as you mentioned in comment #2 this will be a problem...

Thank you!

potss’s picture

$sql = "SELECT max(nid) AS num FROM {node}";
$result = db_query($sql);
$data = db_fetch_array($result);
$id = sprintf("%03d", $data["num"] + 1);
return $id;
asak’s picture

@potss - that's an interesting one...!

Anonymous’s picture

Status: Active » Closed (fixed)

Closing.

hgurol’s picture

Version: 5.x-1.1 » 7.x-1.0-alpha1
Status: Closed (fixed) » Active

When I use the potss on #7, I keep getting HTTP 500 errors when saving a node.

Is there any working code snippets that I can use for D7?

...or is there any better way to use Auto Nodetitles to make the Title field work as a primary key column with auto increment ?
(maybe I should open a new issue for the last sentence)

Thanks...

lightstring’s picture

Subscribing

Zuzuesque’s picture

<?php
$data = db_query("SELECT MAX(nid) FROM {node}")->fetchCol();
$nid = sprintf("%03d", $data[0] + 1);
return $nid;
?>

Code from above transformed to work with Drupal 7. Works on my system.

However I still see a problem with the code... its not atomic. A problem if you want truely unique titles, somewhat okay if you don't. Not really pleased with the solution as of yet.

Anonymous’s picture

Status: Active » Closed (fixed)

Here's the code I just used on a D7 site, if anyone's interested. It's got some comments explaining the different parts, and it works for both creating new nodes and updating the title of existing ones, but is otherwise pretty much the same as above.
I used this module and the PHP filter from core.

// Get NID
if (!empty($node->nid)) {
  // Get current NID if editing an existing node
  $id = $node->nid;
}
else {
  // Generate ID if saving a new node (as NID doesn't exist yet)
  $id = db_query("SELECT MAX(nid) FROM {node}")->fetchField();
  $id++;
}

// Generate title
$title = 'Node ' . $id;

return $title;