Page 1 of 1

alphanumeric increment

Posted: Fri Nov 16, 2007 12:03 pm
by elliebee
Hi guys. I'm hoping you could help me out. I need to generate some ID that is alphanumeric.. Something like

001, 002, 003, ..., 009, 0A0, 0A1, etc....

And I have no idea how to do it. :cry:

Please help!

Posted: Fri Nov 16, 2007 4:45 pm
by deadoralive
You after something like this?

Code: Select all

for( $i = 1; $i <= 50; $i++ )
{
	print str_pad( dechex( $i ), 3, "0", STR_PAD_LEFT) . "<br />";
}
Output:

001, 002, 003, 004, 005, 006, 007, 008, 009, 00a, 00b, 00c, 00d, 00e, 00f, 010, 011, 012, 013, 014, 015, 016, 017, 018, 019, 01a, 01b,
01c, 01d, 01e, 01f......

Re: alphanumeric increment

Posted: Fri Nov 16, 2007 7:44 pm
by califdon
elliebee wrote:Hi guys. I'm hoping you could help me out. I need to generate some ID that is alphanumeric.. Something like

001, 002, 003, ..., 009, 0A0, 0A1, etc....

And I have no idea how to do it. :cry:

Please help!
It might help if we knew what you are trying to do. Is it going to be used as a record identifier in a database table?

Posted: Fri Nov 16, 2007 10:07 pm
by RobertGonzalez
Also, what is the actual logic you are trying to apply. That would be helpful.

Posted: Mon Nov 19, 2007 2:57 am
by elliebee
deadoralive wrote:You after something like this?

Code: Select all

for( $i = 1; $i <= 50; $i++ )
{
	print str_pad( dechex( $i ), 3, "0", STR_PAD_LEFT) . "<br />";
}
Output:

001, 002, 003, 004, 005, 006, 007, 008, 009, 00a, 00b, 00c, 00d, 00e, 00f, 010, 011, 012, 013, 014, 015, 016, 017, 018, 019, 01a, 01b,
01c, 01d, 01e, 01f......


This is actually perfect. Thanks so much!

Posted: Mon Nov 19, 2007 3:01 am
by elliebee
actually... what if i wanted it till 'Z'?

Posted: Mon Nov 19, 2007 4:19 am
by Kieran Huggins
You could use base_convert() to convert your number to decimal (0-9), increment it, then convert back to base 36 (0-z), padding as necessary.