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