TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-09-2008, 02:45 PM   #1 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default 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!)
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 03-09-2008, 02:58 PM   #2 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

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.
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 03-09-2008, 05:18 PM   #3 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

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!
maZtah is offline  
Reply With Quote
Old 03-09-2008, 06:14 PM   #4 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

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'.
sjaq is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:36 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design