What I have is a table with elements. Each element has a unique id and a unique short id, called short. It looks e.g. like the following:
id=>short
1 => 1
9 => 9
10=> a
37=> B
etc. The short ids are generated via a php function.
what I want to do now is letting the user set his own custom short.
Let’s say the 38th user wants his short to be “foobar”. The easy solution would be:
10=> a
37=> B
38=> foobar
39=> D
But that way I gonna lose the C . And if many usersdecide to use a custom short id, that will end up in many short ids lost.
So each custom short will cause the id to be “too far ahead” is there any way to fix this issue? either in mysql or php.
Perfect would be:
10=> a
37=> B
38=> foobar
39=> C
40=> FUBAR
41=> D
Update: (possible triple row approach)
row “used” only increases, when the short id was generated and not custom made. This way the short ids can always be generated using the “used” row. e.g. :
id | short | used
10 | a | 10
37 | B | 37
38 | foobar | 37
39 | C | 38
40 | FUBAR | 38
41 | D | 39
Should be simple enough.
Create a new table with recycled short ids
Create another (sequence) table that holds the last id used for short id generation
When a short id gets replaced by a custom one, insert it into the recycling bin
When you need to create a new short id, look in the recycling bin first.
a. if you can find an item, use that as the new short id (and remove it afterwards)
b. if the bin is empty, increase the sequence by one and generate a new short id from that.
Update
This is a simple way to create and use sequence tables: