Creating Meaninful Customer ID

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply

Rate my first post ettiquette?

Poll ended at Tue May 02, 2006 1:20 pm

Well-articulated and understandable
1
100%
Premature-Study More!
0
No votes
Read this whole site, then come back to me.
0
No votes
 
Total votes: 1

mr. mfs
Forum Newbie
Posts: 1
Joined: Tue Apr 25, 2006 12:41 pm

Creating Meaninful Customer ID

Post by mr. mfs »

Hello All,

Code: Select all

<? php
//Creating unique part of ID
$unique_id_int=0
//Getting two digit value of month
$month=date(m);
//Getting two digit value of year
$year=date(y);
//Creating 'customerid' by concatenating month.year.unique.id.int
$customerid='$month.$year.$unique_id_int';
while ($unique_id_int<=9999);
$unique_id_int++;
//Here is the missing piece for which I need assistance
?>
I am trying to populate automatically generated unique IDs into the Customer ID column in my table. However, I would like to append the two digit value of current month and year to a unique, ascending number up to 9999.

Example: This month's potential range would be 04060000 to 04069999.

I will set the field value to be unique on the table-side. Will this statement automatically generate the next available ID or create an error? Additionally, how can I have the $unique_id_int reset to 0 at month's change?

I am faily new to PHP (but have done some studying and tutorials thus far) so forgive any mistakes I've made.

Thank you kindly,

Michael
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The code you have right now will generate the same exact $customerid every run. Literally it'd be

Code: Select all

$month.$year.$unique_id_int
Why? Because you've used a single quote string. Single quote strings don't parse variables placed in them.

To avoid unnecessary notices from PHP (whether you see them or not,) you should quote string literals, like "m" and "y" in your calls to date(). Provided you have some way of, correctly, keeping the customer number data, it seems okay overall. The idea, that is. What I would likely do is store the year, month and the number in separate fields within the customer record and the last number generated in another table in some fashion.
Post Reply