I have the following function that checks to make sure the code entered matches one in a database.
If it does, it adds one credit to the user, and subtracts one from the corresponding row.
else if ($code_enter != NULL){
$code_check = $this->CI->db->select('code')->from('be_coupons')->where('code', $code_enter)->limit(1)->get();
$credits_list = $this->CI->db->select('*')->from('be_coupons')->where('code', $code_enter)->limit(1)->get();
if ($code_check->row() && $credits_list->row()->credits > 0){
$data['credits'] = ($this->CI->db->select('credits')->where('user_id', $id)->get('be_user_profiles')->row()->credits + 1);
$datas['credits'] = ($this->CI->db->select('credits')->where('code', $code_enter)->get('be_coupons')->row()->credits - 1);
$this->CI->db->update('be_coupons', $datas, array('code' => $code_enter));
$this->CI->home_model->update('UserProfiles',$data, array('user_id' => $id));
flashMsg('success',"WOOT");
redirect('home','location');
}
else if ($code_check->row() && $credits_list->row()->credits < 0){
flashMsg('warning','The code you entered is no longer valid.');
redirect('home/addCredit','location');
}
else{
flashMsg('warning','The code you entered is not valid. Check your entry and try again.');
redirect('home/addCredit','location');
}
}
This code works, but I believe I'm being redundant. Could you streamline this, and make it more elegant? Thanks!