在代码点火器中添加两个数组变量


Additing two array variable in codeigniter

我正在构建一个系统,需要更新数据库字段中的值,但在此之前,我需要获取数据库中的当前值,然后将其添加到当前值中,下面是控制器的代码。

public function transfer_amount(){

    $email  =  $this->input->post('view');
    $this->load->model('user_model');
    $data['user_balance'] = $this->user_model->fetch_balance($email);
    $data['balance'] = $this->input->post('amount');
    $data['total'] = $this->math->add($data['balance'],$data['user_balance']);

    $data = array(
            'balance' => $data['total'],
    );


    if($this->user_model->transfer_amount($data,$email)== true){

        $this->load->view('layer_service/success',$data);

    }
    else
    {

        $this->load->view('layer_service/unsuccessfull',$data);
    }

}

}

那么从数据库中获取当前余额的模块中的代码如下。

 function fetch_balance($email){

    $this->db->select('balance');
    $this->db->from('tbl_users');
    $this->db->where('email', $email);
    $query = $this->db->get();
    $result = $query->result();
    return $result;

}

不确定,但看看这个部分-不要称之为$data,因为我认为这会在以后的中把你搞砸

例如,称之为$balance

  $balance = array(
            'balance' => $data['total'],
    );
    // pass the $balance to your model method
    if($this->user_model->transfer_amount($balance,$email) == true){
       // keeping $data here to pass to the view 
        $this->load->view('layer_service/success',$data);
    }

如果失败,获得余额应该包含在if中

if( ! $data['user_balance'] = $this->user_model->fetch_balance($email) )
{
   $this->_showNoUserFor($email) ; 
} 

另一个建议:

$email  =  $this->input->post('view');

先验证电子邮件,然后再将其发送到数据库表。codeigniter表单验证库运行得非常好。

======

编辑此部分

 $data['total'] =   $this->math->add($data['balance'],$data['user_balance']);

意味着你有一个名为math的模型,它有一个称为add()的方法因此,如果你没有,你只需要使用php数学,这是非常简单的

$data['total'] = $data['balance'] + $data['user_balance'] ; 

当然,这是假设所有的东西都已经过验证,所以你实际上是在把两个数字加在一起。