I currently have this code which is working fine, but I was wondering if I could streamline a little bit. I'm taking data from two tables, merging them with UNION ALL and adding an IF statement that matches the keywords the user input.
this is the current code:
$data['keyword'] = $this->input->get_post('keyword');
// 投稿取得
/* SQL作成 */
$this->db->select("id,article_type,title,main_img1,open_date,contents1");
$this->db->from('column');
if(!empty($data['keyword'])){
$this->db->where('(title like \'%' . $data['keyword'] . '%\' OR description like \'%' . $data['keyword'] . '%\' OR contents1 like \'%' . $data['keyword'] . '%\') ' );
}
$query1 = $this->db->get_compiled_select();
$this->db->reset_query();
$this->db->select("id,article_type,title,main_img1,open_date,contents1");
$this->db->from('cases');
if(!empty($data['keyword'])){
$this->db->where('(title like \'%' . $data['keyword'] . '%\' OR description like \'%' . $data['keyword'] . '%\' OR contents1 like \'%' . $data['keyword'] . '%\') ' );
}
$query2 = $this->db->get_compiled_select();
$this->db->reset_query();
$articles = $this->db->query($query1 . ' UNION ALL ' . $query2);
Is there anyway to put the if statement inside the UNION ALL query at the end? or do I have to repeat it each time? If not possible I guess I could make the IF statement a variable?