On my database I have two tables one called category and one called question.
On the question table there is a column called tag. Which displays the names of the categories the questions are in.
Example Tag's
news, general
codeigniter, php, lounge
Because after each tag there is a comma how can I make sure when I search for questions with the tag codeigniter it will display the questions within those categories.
Currently returns NO result.
My urls look like
http://localhost/project-1/search/?q=codeigniter
and
http://localhost/project-1/search/?q=how+can+i+do+this
Controller
![[Image: 2zVOacJ3QMIM.png]]()
On the question table there is a column called tag. Which displays the names of the categories the questions are in.
Example Tag's
news, general
codeigniter, php, lounge
Because after each tag there is a comma how can I make sure when I search for questions with the tag codeigniter it will display the questions within those categories.
Currently returns NO result.
My urls look like
http://localhost/project-1/search/?q=codeigniter
and
http://localhost/project-1/search/?q=how+can+i+do+this
PHP Code:
public function search_questions($search) {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'question');
$this->db->join($this->db->dbprefix . 'category', 'category.name = question.tag');
$this->db->like('question.title', $search);
$this->db->or_like('question.tag', $search);
$query = $this->db->get();
return $query->result_array();
}
Controller
PHP Code:
<?php
class Search extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->library('user_agent');
if ($this->input->get('q') == FALSE) {
redirect($this->agent->referrer());
}
$this->load->model('answers/answer_model');
}
public function index() {
if ($this->input->post('search')) {
$q = str_replace('+', ' ', $this->input->get('q'));
echo $q;
$data['title'] = 'Questions containing ' . "'" . $q . "'";
$results = $this->search_questions($q);
$data['questions'] = array();
if ($results) {
foreach ($results as $result) {
$data['questions'][] = array(
'question_id' => $result['question_id'],
'title' => $result['title'],
'votes' => $result['votes'],
'answers' => $this->answer_model->count_answers($result['question_id']),
'views' => ''
);
}
}
$data['page'] = 'search/search_results';
$this->load->view($this->config->item('config_template') . '/template/template_view', $data);
}
}
public function search_questions($search) {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'question');
$this->db->join($this->db->dbprefix . 'category', 'category.name = question.tag');
$this->db->like('question.title', $search);
$this->db->or_like('question.tag', $search);
$query = $this->db->get();
return $query->result_array();
}
}
![[Image: 2zVOacJ3QMIM.png]](http://ibin.co/2zVOacJ3QMIM.png)
![[Image: 2zVOmnFXDBfe.png]](http://ibin.co/2zVOmnFXDBfe.png)