$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;
}
所以这是一个搜索查询,首先我们检查一个搜索是否是“全部”,这意味着搜索所有站点,所以我制作了一个由空格分隔的关键字数组,输入到表单中,然后我把它放在一个视图中 preg_match 的 flash 会话,这是用于 preg_match 在生成的结果视图中突出显示文本,所以我将查询连接起来添加更多 LIKE ,所以问题是当我输入 1 个关键字时,它给了我不同的结果还可以,但是当我输入 2 个或多个关键字时,它给了我相同的结果,我的意思是一个完全相同的结果列表,我不明白为什么会这样,在视图中一切正常,问题在这个代码位置,我试图在查询中设置 th DISTINCT 关键字,但它没有帮助......
ps:这是codeigniter,MVC,这是一个控制器,我不使用模型,因为我不想打开很多文件:)