Page 1 of 1

Creating Meaninful Customer ID

Posted: Tue Apr 25, 2006 1:20 pm
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

Posted: Tue Apr 25, 2006 1:58 pm
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.