Good day to everybody
I have a simple counter php+db
it checks ref number of the last content that was added and gives a ref number to newly added content by adding + 1 to the previous ref number of specific content type (order).
it works fine till it gets to ref number 10, then it stock and gives only 10 to content that i add.
so i cant add content with ref number 11 and 12, 13 ,14 …
drupal 7
thank you for your time and help.
if (empty($node->title)){
drupal_set_message('new node');
$query = db_select('node', 'n');
$query->condition('n.type', 'order', '=');
$query->fields('n', array('title'));
$query->orderby('title','DESC');
$query->range(0, 1);
$query->execute();
$result = $query->execute();
$record = $result->fetchAssoc();
drupal_set_message('previous order №: ' .print_r($record['title'], true));
$title = $record['title'] + 1;
drupal_set_message('order №: ' .print_r($title, true));
$form_state['node']->title = $title;
}
Ok, so after a lengthy discussion the answer is:
You cannot use an alphanumerical column type (varchar(255)) and expect an alphanumerical descending sorting to produce the highest number stored inside that column. This is because a leading ‘9’ will always come out before anything else, even if values like 10, 11, 3529 or whatever iares stored inside that column.
You either have to change the column type to a numerical type or use a much more complex sorting strategy. If you cannot change the type of that column then you might want to have a try using type casting in your SELECT query and sort by the result of that type cast. Though such approach is very slow and inefficient.
Also note that your algorithm is unsafe, another insert query might interfere whilst you are still computing your next index if you are working without transactions.