-
Alphanumeric Generator?
Hey,
Right now I am using (http://www.i-fubar.com/random-string-generator.php) but I need to generate the string so it can be used as a Ticket ID.
The only thing is, this ID needs to be unique to the ticket - So how would I go about generating a unique alphanumeric string?
It needs to be like this: LSOK32-HSJK39 (So 6 characters, hyphen then another 6 characters)
I only just realised that the thing generated might not be unique so thats why I am posting.
Simply, My idea was:
Generate
Check if it is already in DB
If so, Regenerate
Else, set it to the query
The only thing is, I dont know how to get the hyphen in and how to regenerate the code?
This sounds all a bit stupid and I presume its pretty easy to do!
Ill try to +Rep all useful answers :)
Moved by Invent (Forum Moderator) from Designing & Development: Please post in the correct forum next time, thanks :).
-
PHP Code:
<?php
$let = 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" );
$random = $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . rand(0, 9) . rand(0, 9) . '-'. $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . rand(0, 9) . rand(0, 9);
$random = strtoupper( $random );
echo $random;
?>
That's one way to generate a random string.
To keep checking & regenerating just make it a function like this:
PHP Code:
function generate()
{
$let = 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" );
$random = $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . rand(0, 9) . rand(0, 9) . '-'. $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . rand(0, 9) . rand(0, 9);
$random = strtoupper( $random );
$query = mysql_query( "SELECT `column` FROM `database` WHERE `column` = '". $random ."'" );
$num = mysql_num_rows( $query );
if( $num > 0 )
{
$random = generate();
}
else
{
return $random;
}
}
So in your code to get a unique code you would just do like $randomString = generate() and then the fuction would keep cycling until it gets a unique variable. (The above code should work).
-
Oh, it has that lol!
Thanks, +REP :)
-
That's what my first code does, lol.
PHP Code:
<?php
$let = 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" );
$random = $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . rand(0, 9) . rand(0, 9) . '-'. $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . $let[ rand(0, 25) ] . rand(0, 9) . rand(0, 9);
$random = strtoupper( $random );
echo $random;
?>