2013-04-15 16 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不是問題。 –