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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)