$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;
}
so this is a search query, first we check if a search is “all”, it means search by all the site, so then i make a array of keywords separated by whitespaces, entered in the form, so 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, so then i make a concatenation of the query be adding more LIKE , so the problem is when i input 1 keyword, it gives me different results and its okey, but when i enter 2 or many keywords, it gives me the same results, i mean a list of result that are exact the same, i can’t understand why is that, in the view its all okay, the problem is in this place of code, i have tried to set th DISTINCT keyword in the query, but it does not helped…
ps: this is codeigniter, MVC, this is a controller, i don’t use models, because i don’t want to open many files 🙂
Check if your
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.