而Else条件PHP

问题描述 投票:0回答:1

我知道我的问题可能与while else statement? PHP相似。但是,该解决方案似乎不适合我的情况。

所以,在我看来是这样的。

  1. 我从数据库中执行select查询以获取所有记录然后获取result in array
  2. 我从阵列中得到了totalperiod
  3. 做检查,当$qtyOut > $total,它将循环{}内的陈述。
  4. 这是问题,在AFTER LOOP ENDS之后,它应该做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'];
                }
            } else{ //when while loop ends, i want it to execute the code here
                $sisa = $total - $qtyOut;
                $this->m1->updateOut1($period, $sisa, $id);
            }
        }

我还是找不到正确的方法,我应该使用另一种循环方法,如何做到这一点?

php codeigniter web while-loop
1个回答
2
投票

如果 - 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,如果不是,那么其他将执行

© www.soinside.com 2019 - 2024. All rights reserved.