Hey,
can anyone help with how to put somethign like the following into a loop, I can't get my head round it, infact, I can't even figgure out which loop type to use:o
//gen randomID
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$length = 40;
$max_i = strlen($chars)-1;
$randomID = '';
for ($i=0;$i<$length;$i++)
{
$randomID .= $chars{mt_rand(0,$max_i)};
}
//see if Random ID is in db
$sql ="SELECT randomID FROM table WHERE randomID='$randomID' LIMIT 1";
$rs = mysql_query( $sql, $con )
or die ('error - sql1');
$num = mysql_num_rows( $rs );
if( $num != 0 )
{
//if it is, generate a new one (and repeat to make that isn't there?)
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$length = 40;
$max_i = strlen($chars)-1;
$randomID = '';
for ($i=0;$i<$length;$i++)
{
$randomID .= $chars{mt_rand(0,$max_i)};
}
}
else
{
//keep $randomID and move on
}
Ultimately all I'm trying to do is:
generate a random string
see if it's already present in a db,
if it is, regenerate
and repeat the process 'til one that isn't present in the DB is generated?
I'd like to understand what I'm doing, so that if I come accross a similar need with modifications leter, I won't need to ask the same questions again, so I'm not really looking for a code example, more a bit of direction on how I should be approacing it (what type of loop to use, should I be using some type of function, etc)
If anyone's able to help, thanks for your time.