1

Is is possible to auto increment an alphanumeric number with php so it looks like:

AB001
AB002
...
...
BA001
...
...
ZZ001

This code will then need to be added to mysql, I was thinking a varchar(5).

Cheers,

fabrik
  • 14,094
  • 8
  • 55
  • 71
Kyle Hudson
  • 898
  • 1
  • 14
  • 26
  • Do you have any attempts at writing a function for it? It is possible, you just have to write the function the handle the incrementing. – Jim Mar 23 '11 at 15:19
  • what do you mean by "this code will then need to be added to mysql"? do you want to insert rows with a field x from `AB001` ... `ZZ001`? – ax. Mar 23 '11 at 15:24
  • @Brad: I just wanted to know if it was possible first – Kyle Hudson Mar 23 '11 at 15:38
  • @ax: basically when an user signups we assign them an unique five digit code which is used for tracking on our platform – Kyle Hudson Mar 23 '11 at 15:39

1 Answers1

7

Try it and see

$x = 'AA998';
for($i = 0; $i < 10; $i++) {
    echo $x++,'<br />';
}

EDIT

Reverse of letters and digits

$x = '000AY';
for($i = 0; $i < 10; $i++) {
    echo $x++,'<br />';
}

or reversal after ZZ999

$x = 'ZZ998';
for($i = 0; $i < 10; $i++) {
    $x++;
    if (strlen($x) > 5)
        $x = '000AA';
    echo $x,'<br />';
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thanks Mark, that worked. Only issue is now how would we go about reversing it so its now 000AA...999AA...000AB...999AB etc? – Kyle Hudson Mar 23 '11 at 15:45