Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14115

Model function not working correct

$
0
0
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


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]

[Image: 2zVOmnFXDBfe.png]

Viewing all articles
Browse latest Browse all 14115

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>