Hi all,
I have a script where new members are issued with a random reference number between 1 and 20. Then their details and ref number are inserted into a mysql db.
I use mt_rand to generate the number but am struggling to find a way of ending this process when all the numbers have been assigned.
this is my code................................
//generate random number and store those already generated in an array
// otherwise when searching for only 1 remaining number it should find it
$randomNumbers = array(); // storage array
for ($i=0;$i<20;$i++) {
$ref = mt_rand(1,20);
while(in_array($ref,$randomNumbers)) {
$ref = mt_rand(1,20); }
array_push($randomNumbers,$ref);
}
require ($_SERVER["DOCUMENT_ROOT"] ."/conf/db_con.php") ;
$connection = mysql_connect ($db_host, $db_user, $db_password) or die ("error connecting") ;
mysql_select_db ($db_name, $connection);
//check number against one already taken
$checkref = mysql_query("SELECT ref FROM fa_users WHERE ref='$ref'");
$ref_exist = mysql_num_rows($checkref);
if($ref_exist > 0){
include 'select.php'; }
As you can see, the if statement checks whether the number has already been assigned and if so, reloads this same page. (is there a better way? i.e: start from the first line of this code??) I don't know how....
And, how do I tell it to stop when all numbers have been taken? At the moment i get a 500 error as it times out obviously because there are no numbers left.
I hope i have explained my problems correctly, any help much appreciated