不能在codeigniter中的foreach里面改变if中变量的值

时间:2018-01-02 19:36:45

标签: php codeigniter

$this->db->where('scheme_id', 1);
                $query = $this->db->get('scheme_installment');
                $input = array();

                // Flag to check if the amount is available to add in customer installment table
                $hasMoreInstallments = TRUE;

                foreach($query->result_array() as $item) {

                    $input['scheme_installment_id'] = $item['scheme_installment_id'];
                    $input['customer_id'] = $insert_id;
                    $input['created_date'] = date('Y-m-d H:i:s');
                    $input['last_modified_date'] = date('Y-m-d H:i:s');

                    $data['down_payment'] -= $item['installment_price'];

                    if( $hasMoreInstallments && $data['down_payment'] >= 0) {

                        $input['paid_date'] = date('Y-m-d H:i:s');
                        $input['comment']   = $hasMoreInstallments.' : '.$data['down_payment'];

                        if($data['down_payment'] >= $item['installment_price'] ) {
                            $input['amount']    = $item['installment_price'];
                            $input['status'] = 'Paid';    
                        } else {
                            $input['amount']    = $data['down_payment'];
                            $hasMoreInstallments = FALSE;
                        }
                    }

                    $query = $this->db->insert('customer_installments', $input);
                }
                return $query;

基于hasMoreInstallments(True / False)我想插入数据。这里我无法停止插入,因为它没有将hasMoreInstallments的值修改为FALSE。每次循环运行时,它都将hasMoreInstallments视为TRUE,尽管记录也会打印在else块上。

2 个答案:

答案 0 :(得分:0)

我认为在将变量减去之前没有将变量设置为值存在问题。

从我看到的变量$data['down_payment']永远不会设置为例如0,如下所示:

$data['down_payment'] = 0;

// now we can subtract from it
$data['down_payment'] -= 5;

但是,如果我们不为变量赋值并尝试从中减去值,则会有未定义的值可能会破坏您的脚本。

答案 1 :(得分:0)

它总是插入因为

$query = $this->db->insert('customer_installments', $input);

if条件

之后
if($hasMoreInstallments && $data['down_payment'] >= 0)`{
  ...
}

//your insert is here