SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple unique code generation

    Hi All,

    Say for example, I own a grocery store, and I own x amount of trolleys, maybe another store has trolleys looking the same as mine, and I want to keep track of my trolleys with use of a database.

    Lets say I have 1000+ trolleys, but now to input a 1000+ trolleys is going to take quite some time. I want to make a PHP script, that will link up to a database, that will in this case, have a form, where I can put in that I have 1000 trolleys, give the trolley description only for the 1st one, as all trolleys look the same, and have the script generate a code for each trolley, but a unique code, and something that can't be guessed as to what the next trolley's code would be.

    So I fill in the form, state in box 1 I have 1000 trolleys, box 2, a trolley description, and a submit button. Now upon clicking submit, the script inputs 1000 records into a database, all 1000 having the same description, at a later stage I could have a smaller trolley for example, which I could specify the types in the database by means of a number, like 1 or 2, but, problem is the generation of the codes.

    I need each trolley to have a UNIQUE code. So lets say trolley 1 gets a code of 12345, trolley 2 can't be 12346, it needs to be something that will be different for each one, not completely different, but hard to 'crack' what the next trolley's code would be. So basically have 1000 generated codes, all done at the same time.

    Basically, the number of trolleys I specify in the form, will obviously be the amount of codes the php script would generate.


    Thanks for any help.

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Could use

    PHP Code:
    $uniqueid microtime_float(); 
    each time you insert into the database.

    or use

    PHP Code:
    $uniqueid rand(); 
    but this would need to be checked against existing ID's as it is possible to obtain the same number twice


    Or just keep adding a random number between 5 and 30 to the last number used

    PHP Code:
    $uniqueid $uniqueid rand(5,30); 
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, will give it a try...

    How do I, in a form, get the list/menu to get its data from a database? like for example if I have multiple branches, and I want to select a branch to add the trolleys to. If all my branches are listed in the database, say table 'branches', and then have a field 'place' for which town it is in. How would I get the drop down menu to get those branch names from the database?

  4. #4
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No worries about the previous question, I came right.

  5. #5
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can somebody please show me an example, of how one can do what I want to do?

    Just a demo, nothing fancy,

    A simple form, with a quantity option, and a name. I want that name to get added into the database the amount of times as is set in the quantity section. Simply inserting the record x amount of times, with the same name as specified in the name option, but with the 3rd option of the generated unique ID using something like "$uniqueid = microtime_float(); ", so that each name has its different ID.

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'll let you put it into a form but heres the basics of the insert loop, not tested of course.

    PHP Code:
    $numtrolleys 100;
    $description 'Fourwheels and handle';
     
    for (
    $count $numtrolleys$count 0$count--) {
     
    $id microtime_float();
     
    $sql mysql_query("INSERT INTO table
         SET TrolleyID = '
    $id',
             Trolleydesc = '
    $description'
             "
    );
    }
    echo 
    'done'
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  7. #7
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome, thank you!

    Another question, using the rand() command, is there a way to force a character length in its generation?

    Like the 1st generated number may be 13, is there a way to make it so it is 0013?

  8. #8
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You could use substr() to shorten the returned string to a set length

    PHP Code:
    substr($id010); // will return first 10 characters 
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  9. #9
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, how would I then specify the rand start value?

  10. #10
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you want to use rand() it will choose a number between 0 & the RAND_MAX set in PHP, no need to give it a seed.

    http://fr3.php.net/rand
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  11. #11
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok...

    I like the way rand is working atm, only problem is the amount of characters it is giving me. Like starting at 0, and going up so lets say 9 is wat it chooses, Now I need it to be 0009.

  12. #12
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP questions? RTFM
    MySQL questions? RTFM

  13. #13
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    As Kromey said use str_pad

    str_pad($id, 10, "0", STR_PAD_LEFT);

    Will make

    12 = 0000000012
    456 = 0000000456

    etc, etc.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  14. #14
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, appreciate the help


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
  •