some loop problems
Posted: Thu Aug 07, 2008 5:47 am
hi there,
i'm creating a script that generate one code of 9 digits using some values, one of that values is a $nrDoc (is a int value from 1 to 9999) a entity and a value introduced by the user, so after i get this values i start to generate that code, first of all i run a query to see if that $nrDoc already exists in DB, if exists i increment that value by 1 until i cant find an equal, then i use that $nrDoc and a user value to create the code, all this in a do {} while loop. Then inside another loop i see if that code is already in DB, if yes i increment $nrDoc again and generate another code, until i have a different one from those in DB ... i hope you can see all understand this messe
what i need,
1º -> check if the $nrDoc already exists in DB, if not create code;
2º -> check if the code already used in DB, if yes increment $nrDoc by 1 and create another one until it doesn't exist in DB;
3º -> if $nrDoc reach all the 9999 combinations it cant allow new codes.
so far this is all i've got, it's working but i can get the same values for nrDoc and for code ...
i know this code is all messed up, and if i have a big number os rows in db this script will be very slow, and other problem is that i can have up to 2 codes in the same row, but i'm out of ideas 
i hope you can give me some ideas.
thanks in avance
i'm creating a script that generate one code of 9 digits using some values, one of that values is a $nrDoc (is a int value from 1 to 9999) a entity and a value introduced by the user, so after i get this values i start to generate that code, first of all i run a query to see if that $nrDoc already exists in DB, if exists i increment that value by 1 until i cant find an equal, then i use that $nrDoc and a user value to create the code, all this in a do {} while loop. Then inside another loop i see if that code is already in DB, if yes i increment $nrDoc again and generate another code, until i have a different one from those in DB ... i hope you can see all understand this messe
what i need,
1º -> check if the $nrDoc already exists in DB, if not create code;
2º -> check if the code already used in DB, if yes increment $nrDoc by 1 and create another one until it doesn't exist in DB;
3º -> if $nrDoc reach all the 9999 combinations it cant allow new codes.
so far this is all i've got, it's working but i can get the same values for nrDoc and for code ...
Code: Select all
$nrDoc = returnFormatedValue(1); // starts in 1
$value = $_GET['user_value'];
$ref = $_GET['ref'];
$upgrade = $_GET['upgrade']; // this inform that in the same row i need to have another code
$entity = ENTITY; // this value is defined as ENTITY
do {
$query = "SELECT * FROM `flags` WHERE `nr_doc`='$nrDoc'";
$result = mysql_fetch_array( mysql_query($query) );
if($result) { //already exists increment 1
$nrDocumento = returnFormatedValue($nrDocumento + 1);
} else { //dont exists set $nrDoc to DB field and create the code
@mysql_query("UPDATE `flags` SET `entity`='$entity', `nr_doc`='$nrDoc' WHERE `ref`='$ref'");
$code = calcCode($nrDoc, $value, $entity);
break;
}
} while($result);
do {
$query1 = "SELECT * FROM `flags` WHERE `code`='$code' OR `code_upgrade`='$code'";
$result1 = mysql_fetch_array( mysql_query($query1) );
if($result1) { //if already in use, increment and create another code
$nrDoc = returnFormatedValue($nrDoc + 1);
$refMB = MB($nrDoc, $value, $entity);
} else { //if not in use update DB
if($upgrade == null) {
@mysql_query("UPDATE `flags` SET `code`='$code' WHERE `ref`='$ref'");
} else {
@mysql_query("UPDATE `flags` SET `code_upgrade`='$code' WHERE `ref`='$ref'");
}
break;
}
} while($result1);
i hope you can give me some ideas.
thanks in avance