Reputation: 1700
Is there any way to avoid using multiple if else? I have this method:
//Answer Question
public function answer()
{
//Validate form input
$this->form_validation->set_rules('question_id', '', 'integer|required');
$this->form_validation->set_rules('answer_text', 'lang:answer_text', 'trim|required|xss_clean');
if ($this->form_validation->run() == true)
{
$question_exists = $this->questions->select_by_id($this->input->post('question_id'), 1);
//Check if question exists
if($question_exists) {
//Add to database
$answer_id = $this->answers->create(
array(
'question_id' => $this->input->post('question_id'),
'answer_text' => $this->input->post('answer_text'),
'commentator_id' => ($this->ion_auth->logged_in()) ? $this->the_user->id : 0,
'answer_date' => time()
)
);
if ($answer_id)
{
$data['answer'] = $this->answers->select_by_id($answer_id);
if($this->ion_auth->logged_in())
$this->users->update(array('answers' => $this->the_user->answers + 1), $this->the_user->id);
//check if answer is posted on a category page or profile page
if ($question_exists->asked_id != 0) {
$this->questions->update(array('question_status' => 1), $question_exists->question_id);
$data['answer_profile'] = true;
} else {
$data['answer_profile'] = false;
}
$this->load->view('blocks/new_answer', $data);
}
}
}
}
As you see I have a lot of if else and braces. Is there any possibility to optimize the code to be more organised?
Upvotes: 1
Views: 133
Reputation: 2218
An attempt by me, but without understanding the full scope...
public function answer()
{
//Validate form input
$this->form_validation->set_rules('question_id', '', 'integer|required');
$this->form_validation->set_rules('answer_text', 'lang:answer_text', 'trim|required|xss_clean');
if ($this->form_validation->run() == true)
{
//just insert instead of getting/checking if exists, i'm assuming it does, since you are passing what appears to be a primary key of a question
$answer = array(
'question_id' => $this->input->post('question_id'),
'answer_text' => $this->input->post('answer_text'),
'commentator_id' => $this->input->post('user_id'), // instead of checking if the users logged on, we add it to a field in the form before hand, user_id if so, 0 otherwise
'answer_date' => time()
);
if($answer_id = $this->answers->create($answer)){
$data['answer'] = $this->answers->select_by_id($answer_id);
$this->users->update(array('answers' => $this->the_user->answers + 1), $this->input->post('user_id')); //check in method update, if user_id == 0, return immediately
$this->questions->update(array('question_status' => 1), $this->input->post('question_id')); //update where qestion id == $this->input->post('question_id') && `asked_column` != 0
$data['answer_profile'] = $this->questions->check_if_asked($this->input->post('question_id'));//method to determine if asked, if this != 0, return true, in the view we can use isset
$this->load->view('blocks/new_answer', $data);
}
}
}
Upvotes: 0
Reputation: 1727
Merely a suggestion, since I don't know your code context.
I do prefer to use private methods to shorten public ones.
(Please, be aware for any reference mistakes)
//Answer Question
public function answer()
{
//Validate form input
$this->form_validation->set_rules('question_id', '', 'integer|required');
$this->form_validation->set_rules('answer_text', 'lang:answer_text', 'trim|required|xss_clean');
if ($this->form_validation->run() && $this->questionExists())
{
$question_exists = $this->questionExists();
//Add to database
$answer_id = $this->addQuestion();
$answer_id ? $data['answer'] = $this->answers->select_by_id($answer_id) : return;
if($this->ion_auth->logged_in())
$this->users->update(array('answers' => $this->the_user->answers + 1), $this->the_user->id);
//check if answer is posted on a category page or profile page
$question_exists->asked_id != 0 ? $this->updateAnswer() : $data['answer_profile'] = false;
$this->load->view('blocks/new_answer', $data);
}
}
private function questionExists()
{
return $this->questions->select_by_id($this->input->post('question_id'), 1);
}
private function addQuestion()
{
$a_id = $this->answers->create(
array(
'question_id' => $this->input->post('question_id'),
'answer_text' => $this->input->post('answer_text'),
'commentator_id' => ($this->ion_auth->logged_in()) ? $this->the_user->id : 0,
'answer_date' => time()
)
);
return $a_id;
}
private function updateAnswer() {
$this->questions->update(array('question_status' => 1), $question_exists->question_id);
$data['answer_profile'] = true;
}
Upvotes: 2