SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Build a unique slug

    Hi,
    I need a method
    to build a slug (it should be unique) to insert in the Db
    I ended up with this code:
    Code PHP:
    public function buildSlug($value)
        {
          $slug = Zend_Filter::filterStatic($value, 'Slug');
          if(null === $this->getBySlug($slug)){
            return $slug;
          }
          $i = 1; 
          while (null !== $this->getBySlug($slug.'-'.$i)) { 
            $i++; 
          }
          return $slug.'-'.$i;
        }

    I'm wondering if there is a better way ?

    Bye

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,880
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    http://php.net/manual/en/function.time.php

    Have you tried using the PHP time() function plus rand()?
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    http://php.net/manual/en/function.time.php

    Have you tried using the PHP time() function plus rand()?
    yep but the top dog want from 1 to n



    Using rand is a worse way imho

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,880
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by whisher View Post
    yep but the top dog want from 1 to n



    Using rand is a worse way imho
    Instead of inserting a slug - how about creating a unique index and retrieving the value using mysql_insert_id()

    or use hash(...), md5(...) or sh1(...). Search for the $result and if it is unique then insert into yor database otherwise try again?





    .
    Last edited by John_Betong; Apr 8, 2010 at 18:25. Reason: added hash(...) etc
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  5. #5
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If all you need is a unique field, and don't care if it's numeric or not, then use an AUTO-INCREMENT MySQL field.

    If you don't want it to be numeric, but don't care for security much (people can guess it), use MD5(AUTO-INCREMENT MySQL field).

    If you care a bit about people not guessing it, use MD5('AUTO-INCREMENT MySQL field'. 'my special key' . time())

    If you want some text that people can "read", you can do this:
    Make your foo field unique.
    generate your text, something like this:
    PHP Code:
    # try 10 times
    $i 0;
    while (!
    mysql_query("INSERT INTO table SET foo = '".getPass()."'") && $i<10) {
        
    $i++;
    }
    # generate a readable text
    function getPass($length 6) {
        
    $conso=array("b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z");
        
    $vocal=array("a","e","i","o","u");
        
    $password="";
        
    srand ((double)microtime()*1000000);
        
    $max ceil($length/2);
        for(
    $i=1$i<=$max$i++) {
            
    $password.=$conso[rand(0,19)];
            
    $password.=$vocal[rand(0,4)];
        }
        return 
    $password;

    If you care about performance, you will make foo an index, generate a few strings, then
    PHP Code:
    SELECT foo FROM table WHERE foo IN ('.join(',',$optionsArray).'); 
    See what was found, and insert something it was not found. (do this in a loop)

    There are many better ways to do this, but creating them 1 by 1 and doing selects in loops is not a good idea.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •