Solved

create auto_increment function using letters instead of numbers

Posted on 2004-08-04
8
806 Views
Last Modified: 2008-01-09
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
Comment
Question by:exp_dk
8 Comments
 
LVL 13

Expert Comment

by:StormyWaters
ID: 11720948
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
 

Author Comment

by:exp_dk
ID: 11720986
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11721087
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

Author Comment

by:exp_dk
ID: 11727058
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
 
LVL 18

Expert Comment

by:arantius
ID: 11743484
<?

$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
 

Author Comment

by:exp_dk
ID: 11743797
arantius >>
That is true, however, it does not give me the opportunity to choose which letters to include in the returned string :-(
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 11754946
Closed, 250 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Moderator
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question