dongquanlin1885 2015-11-17 16:50
浏览 69
已采纳

在codeigniter中添加两个数组变量

I am building a system where i need to update the value in the database field but before doing that i need to take the current value in the database then adding it to the current value the following is a code from the controller.

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);
    }



}

}

then the code in the module that fetch the current balance from the database is as following.

 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;


}
  • 写回答

1条回答 默认 最新

  • douxian1892 2015-11-17 22:14
    关注

    not sure but look at this part - don't call it $data because i think thats messing you up later

    for example call it $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);
    
        }
    

    getting the balance should be wrapped in an if in case it fails

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

    another suggestion:

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

    validate the email first before sending it to your database table. codeigniter form validation library works really well.

    ======

    EDIT ok this part

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

    means that you have a model called math with a method called add() so if you don't have that you would just use php math which is very simple

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

    of course this assumes everything has been validated first so you are actually adding together two numbers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器