有没有办法避免使用多个if else?我有这个方法:
<ul id="tabs">
<li class="active">Description</li><!--
--><li>Reviews</li><!--
--><li>Ask a question</li>
</ul>
如你所见,我有很多if else和braces。是否有可能优化代码以使其更有条理?
答案 0 :(得分:2)
仅仅是一个建议,因为我不知道你的代码背景。
我更喜欢使用私人方法来缩短公共方法。
(请注意任何参考错误)
//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;
}
答案 1 :(得分:0)
我的尝试,但没有全面了解......
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);
}
}
}