Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 832
  • Last Modified:

create auto_increment function using letters instead of numbers

First of all, I'm danish, so sorry for my poor english.

Second; the question:

Most of you have probably heard of the auto_increment function in databases such as MySQL. Every new entry is automaticly given a number, which is one higher than the number before.

What if I need this function, but instead of numbers, it should be with letters.

I've come by a small javascript, which shows the generel idea:
www.firewerx.dk/tegn.htm (takes a few seconds to load)

What i need, is a way of using this script with a database. When i run the script, I need it to:
1: connect to the database (mysql).
2: select the latest added entry.
3: run the script, and making it "1" larger.

Example:
05:45 "abc" is added to the database by user#1
11:29 user#2 adds a new entry. The script connects to the database, finds "abc" (which was the latest entry added), adds "1" to "abc", which gives the string "abd". After which, "abd" is added to the database as a new entry.

The script needs to be using the following letters:
$letters = "abcdefghijkmnpqrstuvwxyz23456789"

The idea is, that after "a" comes "b", after "b" comes "c" [...] after "z" comes "2", after "2" comes "3" [...] after "9" comes "aa", after "aa" comes "ab", after "ab" comes "ac" etc etc etc


If you don't understand a word of this, please feel free to ask :-)


Sincerely
0
exp_dk
Asked:
exp_dk
1 Solution
 
StormyWatersCommented:
If you had it as 0123456789abcdefghijklmnopqrstuvwxyz
You could store it as a normal integer and convert it to base36 when you need it.
I'll try and come up with a way to do what you want, but it'd be easier if you switched to that. I know it's possible though and I'll get back to you.
0
 
exp_dkAuthor Commented:
I'm willing to give it a try, but the thing is, that I've not finally decided, whether or not I want the numbers included at all. I might reconsider, and just go with:
$letters = "abcdefghijkmnpqrstuvwxyz"

I'm not certain yet...

But feel free to let your thoughts wander of :-)
0
 
Marcus BointonCommented:
You can treat it as a base-36 number without worrying about the chars involved - there's nothing stopping from me counting in base 10 using "zu7s05khjt1" instead of 0-9. it really makes no difference, other than ignoring ordinal ASCII values of the chars. I posted some code in this question that allows you to convert from base 10 to and from any base you like, using any sequence of characters as symbols:

http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21073125.html

though this does use 0-9a-zA-Z, that's just the default in the digits function - just change the default string to whatever sequence of chars you like. These functions use the bcmath extension so you can use them to handle numbers of any size - I've used them on 160-bit hashes.

Don't use these values instead of autoincrement in mysql, use them as well as. Relations based on simple integers will always be faster than strings.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
exp_dkAuthor Commented:
I've posted this on another site like this; www.eksperten.dk (danish), and I've got hold on this script, which is doing exactly what I need it to do:

<?php
function spec_code($str, $char) {
    $strlenght  = strlen($str) - 1;
    $charlenght = count($char) - 1;
    $action    = true;
    $result    = '';

    while ($strlenght >= 0) {
        if ($action === true) {
            $key = array_search($str{$strlenght}, $char);
            if ($key < $charlenght) {
                $action = false;
                $result = $char[$key + 1] . $result;
            } else {
                $result = 'a' . $result;
            }
        } else {
            $result = $str{$strlenght} . $result;
        }
        $strlenght--;
    }
    if ($action === true) {
        return 'a' . $result;
    } else {
        return $result;
    }
}

$char      = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
echo spec_code('zza', $char);
?>

Ref:
http://www.eksperten.dk/spm/525946


Thank you for trying, though

Regards
exp
0
 
arantiusCommented:
<?

$a="abc";
$a++;
print $a;
?>


Without numbers, that does it.  Quite simply.  It outputs abd.  If $a were zzz, it would output aaaa.  Just retreieve the last value from the database, put it in a variable and use the ++ operator on the variable.
0
 
exp_dkAuthor Commented:
arantius >>
That is true, however, it does not give me the opportunity to choose which letters to include in the returned string :-(
0
 
ee_ai_constructCommented:
Closed, 250 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now