can I have two while loops with same condition but different if statements in one page?
For instance:
$qtytoAdd = $rec_qty[$mat_key];
while($qtytoAdd > 0){
if(($remBalance - $qtytoAdd) >= 0) {
//do something
$qtytoAdd = 0
}else{
//do something
$qtytoAdd = $qtytoAdd - $remBalance
}
}
And something like this:
while($qtytoAdd > 0){
if(($reqRemBal- $qtytoAdd) >= 0) {
//do something
$qtytoAdd = 0
}else{
//do something
$qtytoAdd = $qtytoAdd - $reqRemBal
}
}
The $qtytoAdd
variable is actually defined at beginning before the loops. What I'm trying to do is run both while loops at the same time with both $qtytoAdd
of each while loops having the same values at first.
Sure but if you have them one after each then to break the first one, you would either need to force a break or make $qtytoAdd != 0
If it is the latter than it would cause the second loop to not be executed.
If you explain what you are trying to do, there should be a more elegant way to do it.
I think what you are actually looking for is just this:
$qtytoAdd = $rec_qty[$mat_key];
while($qtytoAdd > 0){
if(($remBalance - $qtytoAdd) >= 0) {
//do something
$qtytoAdd = 0
}else{
//do something
$qtytoAdd = $qtytoAdd - $remBalance
}
//second block
if(($reqRemBal- $qtytoAdd) >= 0) {
//do something
$qtytoAdd = 0
}else{
//do something
$qtytoAdd = $qtytoAdd - $reqRemBal
}
}
Firstly instead of breaking the loops using $qtytoAdd = 0
you can simply use break
to completely break out of the loop or continue
to skip to the next step in loop.
And then you can merge the the loops in this way: $qtytoAdd = $rec_qty[$mat_key];
while($qtytoAdd > 0){
if (($remBalance - $qtytoAdd) >= 0 || ($reqRemBal- $qtytoAdd) >= 0) {
// Do something
break;
}
if (($remBalance - $qtytoAdd) < 0) {
$qtytoAdd = $qtytoAdd - $remBalance
} elseif (($reqRemBal- $qtytoAdd) < 0) {
$qtytoAdd = $qtytoAdd - $reqRemBal
}
}