Welcome Guest, Not a member yet? Register   Sign In
Codeigniter Search by category and or question title.
#1

(This post was last modified: 04-02-2017, 09:15 PM by wolfgang1983.)

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]
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

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;
  }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB