Re: Creating alphanumeric id for a table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 9:19 PM -0700 9/11/09, aveev wrote:
The reason why I used incremental value from the db is that I need a sequence
number generator that can be used as an argument to my function. If I can
generate sequence number myself, I don't need this incremental value from
db.
What I want is that the sequential alphanumeric id that increments it's
numeric value by one everytime a new record is inserted while the alphabet
part remains the same..(like I've shown before):
AAA0001
AAA0002
...
AAA0009
AAA0010
This id will be used as the application number that will be printed as a
barcode on the user's application form. The requirement says that the app
number is prefixed with the letters (actually it's not 'AAA' but 'FIN' which
stands for File Identification Number)....


Things to consider.

First, if all your application-numbers start with FIN, then my advice would be to do this after you pull the ID from the database. Why store an additional three characters for each ID? And why make your code more complicated than it should be?

Second, considering that you want sequential numbering, are you prepared for what what happens when you delete an item? You either live with gaps in your sequence (recommended) or you renumber.

Third, your example limits the number of applications to 9999 (i.e., AAA9999 (seven characters)), is that correct OR is the next number AAA1000 (eight characters)? If the next number is eight characters, then why not use "AAA1" as the start of the sequence? Why the extra zeros?

Fourth, if you are going to use the index of the table for creating the application-number and require the addition of the alpha prefix, then I would create another field in the table and store the application-numbers there.

For example, I would use (not tested):

// code to create a record (i.e., INSERT INTO your_table (whatever) VALUES ('$whatever') )

$id = LAST_INSERT_ID();

The above statement finds the last record added to the database (the index of the record) and then I would create my application-number and store it in that record -- like so:

// code to create the application-number (i.e., $application-number = 'AAA' . $id; )

$query = "UPDATE your_table SET application_number = '$application_number' WHERE id = '$id' ";
$result  = mysql_query($query) or die('Error, query failed');

That way you should not have any duplications and you have application-numbers (as you want )that are tied to the auto-numbering of the table's index.

HTH's

tedd
--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux