phpmysqlcodeigniterselectsql-like

CodeIgniter query gives unexpected results when using 2 or more search words


$all = $this->input->get('all');

if($all)
{
    $keywords = $this->input->get('search');
    $data['search'] = $keywords;
    $this->session->set_flashdata('search', $keywords);
    $query = "SELECT *  FROM `investOffers`, `news`";
    $counts = "SELECT count(*) as count FROM  `investOffers`, `news`";
    $first = false;
    if($keywords)
    {
        $data['keywords'] = $keywords;
        $this->session->set_flashdata('keywords', $keywords);
        $keywords = explode(" ", $keywords);
        foreach($keywords as $k)
        {
            if(!$first)
            {
                $query .= " WHERE investOffers.desc LIKE '% ".$k." %' OR investOffers.title LIKE '% ".$k." %'";
                $query .= " OR news.desc LIKE '% ".$k." %' OR news.title LIKE '% ".$k." %'";
                $counts .= " WHERE investOffers.desc LIKE '% ".$k." %' OR investOffers.title LIKE '% ".$k." %'";
                $counts .= " OR news.desc LIKE '% ".$k." %' OR news.title LIKE '% ".$k." %'";
                $first = true;
            
            }
            else
            {
                $query .= " OR investOffers.desc LIKE '% ".$k." %' OR investOffers.title LIKE '% ".$k." %'";
                $query .= " OR news.desc LIKE '% ".$k." %' OR news.title LIKE '% ".$k." %'";
                $counts .= " OR investOffers.desc LIKE '% ".$k." %' OR investOffers.title LIKE '% ".$k." %'";
                $counts .= " OR news.desc LIKE '% ".$k." %' OR news.title LIKE '% ".$k." %'";
            }
        }
        
    
    }
        $page = $this->uri->segment(2);
        if($page)
        {
            $query .= " ORDER BY investOffers.date DESC LIMIT ".$page.", 10";
            $counts .= " ORDER BY investOffers.date DESC LIMIT 10";
        }
        else
        {
            $query .= " ORDER BY investOffers.date DESC LIMIT 10";
            $counts .= " ORDER BY investOffers.date DESC LIMIT 10";
        }
    $data['query'] = $this->db->query($query);
    $counts = $this->db->query($counts);
    foreach($counts->result() as $q)
    {
    
    $count = $q->count;
    break;
    
    }
    $config['base_url'] = base_url().'/search';
    $config['prev_link'] = false;
    $config['next_link'] = false;
    $config['last_link'] = false;
    $config['first_link'] = false;
    $config['suffix'] = '?'.http_build_query($_GET, '', "&");
    $config['cur_tag_open'] = '<strong><img src="'.IMAGE.'pagerArrow.png" class="pagerArrow" />';
    $config['cur_tag_close'] = '</strong>';
    $from = intval($this->uri->segment(2));
    $config['per_page'] =  10;   
    $config['num_links'] = 5;    
    $config['uri_segment'] = 2;  
    $config['total_rows'] = $count; 
    $this->pagination->initialize($config);
    $data['pager'] = $this->pagination->create_links();
    $data['content'] = $this->load->view(SITE.'search', $data, true);
    $this->load->view(SITE.'layout', $data);
    return true;

}

This is a search query. First we check if a search is "all", it means search by all the site. Then I make an array of keywords separated by whitespaces, entered in the form. Then I put it in a flash session for preg_match() in the view - this is for preg_match() highlighting text in the view of generated results. Then I make a concatenation of the query by adding more LIKE conditions. When I input 1 keyword, it gives me different results and it's okay. When I enter 2 or more keywords, it gives me the same results - I mean a list of results that are exact the same, I can't understand why that. In the view it's all okay. The problem is in this controller code. I have tried to set the DISTINCT keyword in the query, but it does not helped.

This is a CodeIgniter app and the above code is my conroller method. I don't use models because I don't want to open many files.


Solution

  • Check if your

    $keywords = explode(" ", $keywords);
    

    is returning a length greater then 1. Since you are getting $keywords from a GET, it is possible that you need to urldecode your variable before passing it in the explode function.

    $keywords = urldecode($keywords);