我知道我的问题可能与while else statement? PHP相似。但是,该解决方案似乎不适合我的情况。
所以,在我看来是这样的。
select
查询以获取所有记录然后获取result in array
total
和period
$qtyOut > $total
,它将循环{}
内的陈述。public function trialOut($id, $qtyOut) { $a = $this->uri->segment(3); $dataset = $this->m1->trial($a); $i = 0; $sisa; $total = $dataset[$i]['total']; $period = $dataset[$i]['periode']; if($qtyOut > $total){ while ($qtyOut > $total) { $qtyOut = $qtyOut - $total; $this->m1->updateOut2($period, $id); $i++; $total = $dataset[$i]['total']; $period = $dataset[$i]['periode']; } } else{ //when while loop ends, i want it to execute the code here $sisa = $total - $qtyOut; $this->m1->updateOut1($period, $sisa, $id); } }
我还是找不到正确的方法,我应该使用另一种循环方法,如何做到这一点?
如果 - else语句正在按此逻辑工作。 if if语句下的条件是真的代码insde {} if(condition) {//this code is executed}
将执行。但是,如果condition = false,则执行else下的代码。考虑到这一点,你的其他声明将不会在$qtyOut > $total
时执行。
如果你想在if语句之后执行它,只需删除else和括号。
如果你想在结束之后执行它,试试这个
public function trialOut($id, $qtyOut)
{
$a = $this->uri->segment(3);
$dataset = $this->m1->trial($a);
$i = 0;
$sisa;
$total = $dataset[$i]['total'];
$period = $dataset[$i]['periode'];
if($qtyOut > $total){
while ($qtyOut > $total) {
$qtyOut = $qtyOut - $total;
$this->m1->updateOut2($period, $id);
$i++;
$total = $dataset[$i]['total'];
$period = $dataset[$i]['periode'];
}
$sisa = $total - $qtyOut;
$this->m1->updateOut1($period, $sisa, $id);
}
}
如果不解释
举个例子
x = 5;
if( x = 5 ){
echo 'x is 5';
}
if( x != 5){
echo 'x is not five'
}
是一样的代码
x = 5;
if( x = 5 ){
echo 'x is 5';
}else{
echo 'x is not five'
}
在第一种情况下,您正在检查x = 5,然后如果x不等于5.在第二种情况下,您正在检查x = 5,如果不是,那么其他将执行