PDA

View Full Version : Alphanumeric Generator?



Johno
07-08-2008, 06:43 PM
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 :).

Invent
07-08-2008, 06:53 PM
<?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:



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).

Johno
07-08-2008, 06:59 PM
Oh, it has that lol!

Thanks, +REP :)

Invent
07-08-2008, 07:00 PM
That's what my first code does, lol.


<?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;

?>

Want to hide these adverts? Register an account for free!