?
Solved

create auto_increment function using letters instead of numbers

Posted on 2004-08-04
8
Medium Priority
?
816 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

764 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