Page 1 of 1

Random Account Number Generation

Posted: Tue Oct 14, 2003 8:29 am
by Nay
Here's what I've wrote so far:

Code: Select all

<?php
$numbers = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");

for($i=0;$i<9;$i++) {

$number = array_rand($numbers, 1);
$rand_acc .= $number;

}

$q = "SELECT * FROM customers WHERE acc_no = '$rand_acc'";
$r = mysql_query($q, $connection);
if(mysql_num_rows($r) == "0") {
return TRUE;
} else {
return FALSE;
}
?>
Okay, so that'll work fine. But what would I do if the account number generated matched another one and returened false? How can I put into a loop and keep on generating and checking until I get a valid number.

Though it might be impossible to get a duplicate, I just wouldn't want to take risks in having two different accounts with the same number.

-Nay

Posted: Tue Oct 14, 2003 9:48 am
by liljester
mabe something like this? it would loop until it found a number that didnt match a row in the db.

Code: Select all

<?php
$numbers = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); 


while($valid_acc == false){

  for($i=0;$i<9;$i++) { 
    $number = array_rand($numbers, 1); 
    $rand_acc .= $number; 
  } 

  $q = "SELECT * FROM customers WHERE acc_no = '$rand_acc'"; 
  $r = mysql_query($q, $connection); 
  if(mysql_num_rows($r) == "0") { 
    return TRUE; 
  } else { 
    $valid_acc = FALSE; 
  } 

}
?>

Posted: Tue Oct 14, 2003 10:25 am
by Nay
OMG! Yes! haha........

Thanks, I've been stuck with for loops for too long.

-Nay