0

このコードに問題はありますか?

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;
}

両方の条件が機能しているように見えますが、新しい値を割り当てる部分は機能して$qtytoAddいません。そして、ループを継続させるために return 関数を使用する必要がありますか? 私は初心者です。助けてください。

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.'');
}

これが実際のコード全体です。:]

4

1 に答える 1

1

ループを続行するには、return関数を使用する必要がありますか?

ループを続けるために戻る必要はありません。現在の関数から戻って戻る場合は、returnを使用します。

関数内から呼び出された場合、returnステートメントは現在の関数の実行をただちに終了し、その引数を関数呼び出しの値として返します。returnは、eval()ステートメントまたはスクリプトファイルの実行も終了します。

PHPとクエリ

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;
    }
}

あなたが間違っているところ

コードによると、最初のifがtrueの場合、$qtytoAddに変更を加えません。さらに、関数から戻ります。したがって、whileループは1回だけ実行されます。

于 2013-01-31T15:42:13.993 に答える