TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   unique ID whit numbers and letters? (http://www.talkphp.com/general/2440-unique-id-whit-numbers-letters.html)

marxx 03-09-2008 02:45 PM

unique ID whit numbers and letters?
 
Hi all!

Today my problem is to generate unique ID which contains numbers and letters (ie. 101MH).

As far as I know (well, i don't know much tho), mysql wont let use auto-increment with letters? So, here is couble thoughts what came up but don't quite know how to make these.

First, script checks all IDs from database and picked up biggest ID.
Code:

100MH
101MH
102MH
103MH <- this one

after that, we would use that, explode it and increase that number to 104, add MH again to it and use it as new unique ID.

Second thing what came in my mind was use do while??

So if someone has some suggestions, please could you share it for me? =)

And I know, would be simple to NOT use those letters but I have my reasons! ;)


Thanks for all help (again!) ^^

freenity 03-09-2008 02:58 PM

the letters don't change??
I guess the best way would be storing only numbers in the db, and then when you select or refer to the id add MH at the end.

maZtah 03-09-2008 05:18 PM

I agree with freenity if the letters don't change.

Else i would do something like:

PHP Code:

// first select the last id
$szQuery 'SELECT id FROM table ORDER BY id DESC LIMIT 1';
$pResult mysql_query($szQuery);

// fetch the result
$aRow mysql_fetch_array($pResult);

// get the integer and the letters (there are much better ways to do this, but can't come up with one right now;()
$iId substr($aRow['id'],0,strlen($aRow['id'])-2);
$szLetters substr($aRow['id'],-2)

// add 1 to the integer
$iId += 1;

// add the letters again
$szId $iId $szLetters;

// and finally, insert the new id
$szQuery 'INSERT INTO table (id) VALUES ("'.$szId.'");
mysql_query($szQuery); 

Well, just typed in this textarea, haven't tested it, but you should figure any bugs out!

Good luck!

sjaq 03-09-2008 06:14 PM

I made a class a while ago, which generates strings from id's and id's from strings. The coding is pretty crappy, but I was just beginning with php then, so please spare me :).

Here it is:
PHP Code:

<?php 

    
class GenID {
        
        
// array of usable characters, mix them together to get more random results
        // you can add uppercase chars to increase the number of possibilities
        
private $useChars = array(
            
26 => 'a',
            
11 => 'b',
            
=> 'c',
            
20 => 'd',
            
10 => 'e',
            
18 => 'f',
            
25 => 'g',
            
=> 'h',
            
30 => 'i',
            
=> 'j',
            
32 => 'k',
            
=> 'l',
            
23 => 'm',
            
14 => 'n',
            
28 => 'o',
            
16 => 'p',
            
33 => 'q',
            
=> 'r',
            
19 => 's',
            
=> 't',
            
21 => 'u',
            
29 => 'v',
            
34 => 'w',
            
24 => 'x',
            
=> 'y',
            
12 => 'z',
            
27 => 1,
            
15 => 2,
            
22 => 3,
            
=> 4,
            
31 => 5,
            
=> 6,
            
17 => 7,
            
13 => 8,
            
35 => 9,
            
=> 0
        
);
        
        
// reserved id's with the id that generates it as key
        
private $reserved = array(
            
'home' => 177094,
            
'index' => 51067968
            
'user' => 1004764,
            
'users' => 36171523,
            
'admin' => 44634038,
            
'porn' => 782942,
            
'about' => 44220282,
            
'id' => 1100,
            
'get' => 32766,
            
'in' => 1094,
            
'out' => 37050,
            
'search' => 1166872035
        
);
        
        private 
$linkCCount;
        private static 
$instance;
        
        private function 
__clone() {}
        
        public static function 
get($id) {
            if(
self::$instance === null) {
                
self::$instance = new GenID;
            }
            return 
self::$instance->run($id);
        }
        
        private function 
run($id) {
            if(
is_string($id)) {
                return 
$this->strtoid($id);
            } else {
                
$id intval($id);
                return 
$this->idtostr($id);
            }
        }
        
        private function 
fixId($id) {
            if(
in_array($id$this->reserved)) {
                
$nid $id+1;
                return 
$this->fixId($nid);
            } else {
                return (int)
$id;
            }
        }
        
        private function 
clean($url) {
            return 
preg_replace('/([\W\s])/'''$url);
        }
        
        private function 
idtostr($id$nofix false) {
            if(
$nofix == false) {
                
$id $this->fixId($id);
            }
            if(!isset(
$this->linkCCount)) {
                
$this->linkCCount count($this->useChars);
            }
            
$this->link '';
            
$this->idtostrsub($id);
            return 
strrev($this->link);
        }
        
        private function 
strtoid($str) {
            if(!isset(
$this->linkCCount)) {
                
$this->linkCCount count($this->useChars);
            }
            
$str strtolower($str);
            
$str $this->clean($str);
            if(
strlen($str) === 0) {
                return 
false;
            } elseif(
strlen($str) === 1) {
                return 
array_search($str$this->useChars);
            }
            
$chars = array();
            foreach(
str_split($str) as $k => $v) {
                if(
in_array($v$this->useChars)) {
                    
$chars[] = array_search($v$this->useChars);
                }
            }
            
$s $chars[0];
            unset(
$chars[0]);
            
// start the math

            
foreach($chars as $char) {
                
$s = ($s $this->linkCCount) + $char;
            }

            return 
$s;
        }

        private function 
idtostrsub($id) {
            
$id = (int)$id;
            
// if the id is smaller then the amount of chars just give it a char
            
if($id $this->linkCCount && $id >= 0) {
                if(
$id == $this->linkCCount$id 0;
                
$this->link .= $this->useChars[$id];
            } else {
                
// devide the id by the amount of chars
                
$flr floor($id $this->linkCCount);
                
$res $id - ($flr $this->linkCCount);
                if(
$res == $this->linkCCount) { $res 0; }
                
$this->link .= $this->useChars[$res];
                
$this->link .= ($flr $this->linkCCount && $flr >= 0)? 
                
/* true */    $this->useChars[$flr] :
                
/* false */    $this->idtostrsub($flr);
            }
        }
        
    }
    
    
/* Example Usage */
    
    
$id GenID::get('henkifsf'); // result: 257762013534
    
$str GenID::get(54646446); // result: ks4pi

?>

It saves a lot of characters, as you can see in the example the id: 257762013534 has the string 'henkifsf'.


All times are GMT. The time now is 08:53 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0