0

are/is there any problem with this code?

while($qtytoAdd > 0) {
    if(($remBalance - $qtytoAdd) >= 0) {
        mysql_query('UPDATE `estimates` SET `qty_rec` = `qty_rec` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
        mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = `current_rec_qty` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
    } else {
        mysql_query('UPDATE `estimates` SET `qty_rec` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
        mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
        $qtytoAdd = $qtytoAdd - $remBalance;
    }
    return $qtytoAdd;
}

Both conditions seems to be working but the part where I'll assign the new values for $qtytoAdd is not. And do I need to use the return function to let it continue looping. I'm a newbie. Please help.

foreach($mat_desc as $mat_key => $materials){
    $qtytoAdd = $rec_qty[$mat_key];
    $remBalance = mysql_result(mysql_query('SELECT `est_qty` - `qty_rec` as balance FROM `estimates` WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `qty_rec` <> `est_qty` ORDER BY `item_id` ASC'), 0);
    $balid = mysql_result(mysql_query('SELECT `est_id` FROM `estimates` WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `qty_rec` <> `est_qty` ORDER BY `item_id` ASC'), 0);
    $currRec = mysql_result(mysql_query('SELECT `qty_rec` FROM `estimates` WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `qty_rec` <> `est_qty`'), 0);
    $currid = mysql_result(mysql_query('SELECT `req_id` FROM `requestdetails` JOIN `request` USING(`req_id`) WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_qty` <> `current_rec_qty` ORDER BY `req_id` ASC'), 0);

    mysql_query('INSERT INTO `receivedetails` (`rec_id`,`mat_id`,`rec_qty`) VALUES ('.$rec_id.','.mat_id_from_mat_desc($materials).','.$qtytoAdd.')');

    while($qtytoAdd > 0) {
        if(($remBalance - $qtytoAdd) >= 0) {
            mysql_query('UPDATE `estimates` SET `qty_rec` = `qty_rec` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
            mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = `current_rec_qty` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
            $qtytoAdd = 0;
        } else {
            mysql_query('UPDATE `estimates` SET `qty_rec` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
            mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
            $qtytoAdd = $qtytoAdd - $remBalance;
        }
    }

    mysql_query('UPDATE `estimates` SET `qty_onhand` = `qty_rec` - `qty_rel` WHERE `proj_id` = '.$proj_id.' AND `qty_rec` <> `est_qty` AND `mat_id` = '.mat_id_from_mat_desc($materials).' AND `est_id` = '.$balid.'');
}

This is actually the whole code. :]

8
  • 2
    return will end whatever function you're in - it's not used for controlling loops. What is the rest of the function this loop is part of? Commented Jan 31, 2013 at 15:35
  • 3
    Cue the stock comment to AVOID mysql_query .... it is deprecated Commented Jan 31, 2013 at 15:35
  • @Crontab but whenever I remove the return the table that I'm updating is getting messed up. Commented Jan 31, 2013 at 15:39
  • Your code can gain legibility and portability by setting up the SQL as string variables. $SQL_1 = "Update ... " Commented Jan 31, 2013 at 15:41
  • 1
    Like I asked, what is the rest of the surrounding code? It's near impossible to give you an appropriate answer with only a small snippet of code. Maybe return is appropriate, maybe break is. Maybe you need to change the condition of your while statement. Commented Jan 31, 2013 at 15:42

1 Answer 1

1

do I need to use the return function to let it continue looping

You don't need a return to continue to loop. You use return when you want to return from the current function and go back.

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return will also end the execution of an eval() statement or script file.

PHP & Query

while($qtytoAdd > 0) {
    if(($remBalance - $qtytoAdd) >= 0) {
        mysql_query('UPDATE `estimates` SET `qty_rec` = `qty_rec` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
        mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = `current_rec_qty` + "'.$qtytoAdd.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
        $qtytoAdd = $qtytoAdd - $remBalance; // this part does not exist in your code.
        ^^^^^^^^^^^^^^^^^^^^^^-------- depends on your requirement. I added the same line.  
 } else {
        mysql_query('UPDATE `estimates` SET `qty_rec` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `est_id` = "'.$balid.'"');
        mysql_query('UPDATE `requestdetails` SET `current_rec_qty` = "'.$remBalance.'" + "'.$currRec.'" WHERE `proj_id` = "'.$proj_id.'" AND `mat_id` = "'.mat_id_from_mat_desc($materials).'" AND `req_id` = "'.$currid.'"');
        $qtytoAdd = $qtytoAdd - $remBalance;
    }
}

Where you have gone wrong

According to your code if the 1st if is true you don't make changes to the $qtytoAdd. Plus you return from the function. So the while loop will be executed only once.

Sign up to request clarification or add additional context in comments.

1 Comment

If the $qtytoAdd reassignment lines are the same, it should be placed outside of the conditional inside the while (instead of duplicated in both blocks of the conditional).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.