some loop problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

some loop problems

Post by bouncer »

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

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 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: some loop problems

Post by onion2k »

Why not just select the maximum code from the database and add 1 to it? "SELECT MAX(`nr_doc`)+1 AS `max_nr_doc` FROM `flags` GROUP BY `nr_doc`".. no need to loop then.

Of course, if you need to reuse numbers for any reason that wouldn't work...
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Re: some loop problems

Post by bouncer »

onion2k wrote:Why not just select the maximum code from the database and add 1 to it? "SELECT MAX(`nr_doc`)+1 AS `max_nr_doc` FROM `flags` GROUP BY `nr_doc`".. no need to loop then.

Of course, if you need to reuse numbers for any reason that wouldn't work...
when it reachs 9999, i'll turn all nrDoc field to NULL and use another entity to create the code.

thanks allot onion2k, that realy helped me, simple and effective. :D
Post Reply