У меня есть следующая функция, которая проверяет, совпадает ли введенный код с кодом в базе данных.
Если это так, он добавляет один кредит пользователю и вычитает один из соответствующей строки.
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');
}
}
Этот код работает, но я считаю, что я избыточен. Не могли бы вы упростить это и сделать его более элегантным? Спасибо!