Solved

create auto_increment function using letters instead of numbers

Posted on 2004-08-04
8
798 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:Squinky
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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‚Ķ
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now