4

Like the title suggests I need to do something like so...

$i++;//we all know this.

$value = 'a';

increment($value);// i need this functionality
//output 
string [ b ]

///here are some more samples, to help you understand...

increment('b'); //output// c
increment('z'); //output// A [capital A not fussy but would be good :) ]
increment('9'); //output// a1
increment('a1'); //output// a2
increment('aa1'); //output// aa2

and so on... UPDATE

well lets say I use numeric values $id++; I would end up with a massive number eventuall 1310743942525; which can take alot more space than say `ab34c9" im trying to save length of characters to save on db ...

Val
  • 17,336
  • 23
  • 95
  • 144
  • 1
    Can you provide a better map of what you are looking for? a-b, then A-Z, then 0-9, then what? I see you have 9 to a1, but I don't quite understand your pattern then. – Brad Jul 15 '11 at 15:27
  • What are you attempting to accomplish? Maybe with a better understanding of the problem, a simpler solution can be had. – AndrewR Jul 15 '11 at 15:29

3 Answers3

3

You try to treat it as a base 62 number:
http://www.pgregg.com/projects/php/base_conversion/base_conversion.php

with source code at

http://www.pgregg.com/projects/php/base_conversion/base_conversion.inc.phps

convert it to decimal, increment, and convert it back to base 62

UPDATE

From how I read the code, you could have a workflow like this:

$value = 'ab8Zb';
$value_base10 = base_base2dec($value, 62);
$value_base10++;
$value = base_dec2base($value_base10, 62); // should be 'ab8Zc'
H Hatfield
  • 881
  • 5
  • 9
2

If all you are trying to do is save database space, consider this.

In MySQL you can have a field with a type UNSIGNED BIGINT. The maximum size of this field is 18446744073709551615 and the storage space is only 8 bytes.

If you were to convert this number (1.844 x 10^19) to base-62, it would be represented as LygHa16AHYF. You would need a CHAR(11) (11 bytes) or a VARCHAR(11) (12 bytes) in order to store the converted number.

If you used VARCHAR for the field type, smaller numbers would take less space, but for the larger numbers it actually takes more. 8 bytes for a huge number is pretty minimal anyway. I would save the effort and just make the DB field a UNSIGNED BIGINT.

AndrewR
  • 6,668
  • 1
  • 24
  • 38
  • 8bytes * 10 000 records will add up my friend, thats the id only, and my client has 5k users already the ID alone will make that a nightmare :) – Val Jul 15 '11 at 16:00
  • If you are only talking about 5k-10k ids (I am assuming they are all sequential) then use a `UNSIGNED SMALLINT` which is only 2 bytes and has a max value of `65535`. If you ever get beyond that, just change the data type to `UNSIGNED MEDIUMINT` which is 3 bytes and goes up to `16777215`. – AndrewR Jul 15 '11 at 16:06
  • I know what you mean I have thought about that, the fact is that it's complicated, it could be something that could host millions of records per day – Val Jul 15 '11 at 16:11
  • If this is going to be used as any sort of foreign key in a database, you want to have it be some type of int. Also, trying to save space when you only have 5k users sounds a little like pre-mature optimization. If you're going to be sending this data in string format e.g. JSON, then the converted value will always save you space. Otherwise, an int or long may be better in a lot of cases. – H Hatfield Jul 15 '11 at 16:12
  • well my friend it's a friday night :) wish I was home or out, but the fact is that we don't always make the websites we edit and try to make it work :) I do use bigint and alot, and db opt because I deal with massive data day in day out, the problem is complicated, and a hight base value could save the day :) – Val Jul 15 '11 at 16:18
0

You can use ascii codes of each letter. This is just a simple example that will show you the idea, ofcourse it need a lot of modyfications if you want to increment 'aa1' into 'aa2' but impossible is nothig ;P You will just need to write few conditions.

function increment($value) 
{
  if(strlen($value)>1)
    return false; 

  $asciiCode = ord($value); 
  return chr($asciiCode + 1); 
}

http://www.asciitable.com/ - ASCII codes table :)

Norbert Orzechowicz
  • 1,329
  • 9
  • 20
  • they do not have to be `aa2` just want it to increment by one each time...its like numbers have a base of 10 digits, alpha numeric can have a base of 36 digits ... if that makes sense at all – Val Jul 15 '11 at 15:40
  • As I said, this method need a lot of modyfications and many coditions to handle $value with more than 1 char. – Norbert Orzechowicz Jul 15 '11 at 15:41