CodeIgniter Forums
Codeigniter Search by category and or question title. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Codeigniter Search by category and or question title. (/showthread.php?tid=67735)



Codeigniter Search by category and or question title. - wolfgang1983 - 04-02-2017

I am trying to be able to create a search model function all in one.

Where if user types in some thing like 

how to

It will search for that match title in questions

But the if the user would like to search for all questions in a category it will display all questions belong to that category.

I have this model function


PHP Code:
public function getQuestions($search NULL) {
$this->db->select('*');
$this->db->from('questions');
$this->db->join('categories''categories.category_id = questions.question_id');
$this->db->join('questions_category''questions_category.category_id = categories.category_id');
$this->db->like('questions.title'$search'match');
$this->db->or_like('categories.name'$search'match');
$query $this->db->get();

if (
$query->num_rows() > 0) {
return 
$query->result_array();
} else {
return 
false;
}


I have also tried using or_where


PHP Code:
public function getQuestions($search NULL) {
 
$this->db->select('*');
$this->db->from('questions_category');
$this->db->join('categories''categories.category_id = questions_category.question_id');
$this->db->join('questions''questions.question_id = questions_category.question_id');
$this->db->like('questions.title'$search'match');
$this->db->or_where('categories.name'$search);
$query $this->db->get();

if (
$query->num_rows() > 0) {
return 
$query->result_array();
} else {
return 
false;
}



It seems to display it twice as shown in image and not correct questions 

Quote:Question: if use trys to search for a question it will try and find it in questions but if the user searches for a category it will display all questions belong to that category how can I make that work in one model function using join


Search url when searching for all questions in category


Code:
http://localhost/project-ask/questions/tagged/codeigniter


[Image: search1_zpsj70wr3tp.png]


Search URL when searching for question

Code:
http://localhost/project-ask/questions/tagged/how-to


[Image: search2_zpsytxf0ujh.png]


[Image: search3_zpsbzwlvszj.png]


RE: Codeigniter Search by category and or question title. - wolfgang1983 - 04-02-2017

I think I may have it working now


PHP Code:
public function getQuestions($search NULL) {
  
$this->db->select('*');
  
$this->db->from('questions q');
  
$this->db->join('questions_category qc''qc.question_id = q.question_id''LEFT');
  
$this->db->join('categories c''c.category_id = qc.category_id''LEFT');

  
$this->db->or_like('q.title'$search'match');
  
$this->db->or_where('c.name'$search);
  
$query $this->db->get();

  if (
$query->num_rows() > 0) {
    return 
$query->result_array();
  } else {
    return 
false;
  }