2013-04-15 14 views
1

我有一个搜索表单来搜索我的数据库中的工厂。当您第一次访问该网站时,我的工厂表中有一个名为“邮编”的字段,您必须输入邮政编码。这将被保存在一个cookie中,所以你不必再次改变它。在codeigniter中搜索两个输入字段

现在我想搜索使用一个搜索关键词和饼干“邮政编码”

这里的工厂是我searchform http://kees.een-site-bouwen.nl/

因此,像这样:

<form action="home/searchresults" method="post"> 
<input type="search" placeholder="search..." name="search"> 
<input type="search" value="<?= $this->input->cookie('postcode')?>"> 
</form> 

,并在我的控制器东西如:

$match = $this->input->post('search'); 
$match2 = $this->input->cookie('postcode'); 

$data['query'] = $this->bedrijven_model->get_search($match, $match2); 
$this->load->view('views/header'); 
$this->load->view('views/searchresults', $data); 
$this->load->view('views/footer'); 

在我的模型中我有:

function get_search($match) 
{ 
    $this->db->like('Bedrijfsnaam', $match); 
    $this->db->or_like('Postcode', $match); 
    $this->db->or_like('Plaats', $match); 
    $this->db->or_like('Telefoonnummer', $match); 
    $this->db->or_like('Email', $match); 
    $this->db->or_like('Website', $match); 
    $this->db->or_like('Profiel', $match); 
    $this->db->or_like('Adres', $match); 
    $this->db->or_like('Categorie', $match); 

    $this->db->join('bedrijven', 'bedrijfcategorieen.idbedrijven = bedrijven.idbedrijven'); 
    $this->db->join('categorieen', 'bedrijfcategorieen.idcategorieen = categorieen.idcategorieen'); 
    $this->db->group_by('bedrijfcategorieen.idbedrijven', 'bedrijfcategorieen.idcategorieen'); 

    $query = $this->db->get('bedrijfcategorieen'); 

    return $query->result(); 
    } 

但这不起作用。 我在这里问了一个类似的问题:Prevent overriding queries in controller function - codeigniter

但没有答案,为我工作。

回答

1

为您提供$this->bedrijven_model->get_search($match, $match2); 2个PARAMS,但后来只提供一个在函数中:get_search($match)。所以:

function get_search($match, $match2 = false) 
{ 
    $this->db->like('Bedrijfsnaam', $match); 
    $this->db->or_like('Postcode', $match); 
    //etc 
    if($match2){ 
     $this->db->where('Postcode', $match2); 
    } 
} 

...应该工作吗?

修订

在聊天很明显,这是最好不要使用活动记录,因为它是一个有点复杂的查询。相反,我提出了这样的原始SQL:

$query = "SELECT * FROM (`bedrijfcategorieen`) 
JOIN `bedrijven` ON `bedrijfcategorieen`.`idbedrijven` = `bedrijven`.`idbedrijven` 
JOIN `categorieen` ON `bedrijfcategorieen`.`idcategorieen` = `categorieen`.`idcategorieen` 
WHERE (`Bedrijfsnaam` LIKE '%".$this->input->post('search')."%' 
OR `Plaats` LIKE '%".$this->input->post('search')."%' 
OR `Telefoonnummer` LIKE '%".$this->input->post('search')."%' 
OR `Email` LIKE '%".$this->input->post('search')."%' 
OR `Website` LIKE '%".$this->input->post('search')."%' 
OR `Profiel` LIKE '%".$this->input->post('search')."%' 
OR `Adres` LIKE '%".$this->input->post('search')."%' 
OR `Categorie` LIKE '%".$this->input->post('search')."%') 
AND (`Postcode` = `".$this->input->post('cookie')."`) 
GROUP BY `bedrijfcategorieen`.`idbedrijven`"; 

......与此位检索结果集:

$result = $this->db->query($query); 

$this->input->post('search')$this->input->post('cookie')可以在上面的SQL $match$match2所取代。我还建议设置Cookie的默认值,因为它可以从用户的输入字段中删除;像

$match2 = $this->input->post('cookie') ? $this->input->post('cookie') : 'default'; 
+0

会试试这个。 :) –

+0

它不起作用。只有$匹配确实有效。 $ match2不是只搜索1个参数 –

+0

如果你将一个邮编硬编码到你的函数中,get_search($ match,'your_postcode')'会发生什么? – Mudshark

0

如果我理解你是正确的,那么为什么你不要在类似的陈述之后再添加一个where语句。这样的事情:

function get_search($match,$postcode) 
{ 
    $this->db->like('Bedrijfsnaam', $match); 
    $this->db->or_like('Postcode', $match); 
    $this->db->or_like('Plaats', $match); 
    $this->db->or_like('Telefoonnummer', $match); 
    $this->db->or_like('Email', $match); 
    $this->db->or_like('Website', $match); 
    $this->db->or_like('Profiel', $match); 
    $this->db->or_like('Adres', $match); 
    $this->db->or_like('Categorie', $match); 

$ this-> db-> where(“postcode”,$ postcode);

$this->db->join('bedrijven', 'bedrijfcategorieen.idbedrijven = bedrijven.idbedrijven'); 
    $this->db->join('categorieen', 'bedrijfcategorieen.idcategorieen = categorieen.idcategorieen'); 
    $this->db->group_by('bedrijfcategorieen.idbedrijven', 'bedrijfcategorieen.idcategorieen'); 

    $query = $this->db->get('bedrijfcategorieen'); 

    return $query->result(); 
    } 

更新:

是您的Cookie设置?你能正确的得到这个价值吗?尝试下一个脚本作为控制器。应该在屏幕上回显cookie值。也许它已经错误地获取cookie值。

$match = $this->input->post('search'); 
    $match2 = $this->input->cookie('postcode'); 
echo "cookie value = ".$match2; 

    $data['query'] = $this->bedrijven_model->get_search($match, $match2); 
    $this->load->view('views/header'); 
    $this->load->view('views/searchresults', $data); 
    $this->load->view('views/footer'); 
+0

会试试这个。它看起来相当我想要的。 –

+0

它只适用于一个参数。我不知道为什么。也许这是我的表单问题。 –

+0

看看http://kees.een-site-bouwen.nl/当你在对话框中设置一个cookie时,你可以在主页的搜索框中看到它。所以抓取cookie不是问题。 –