2013-04-15 63 views
1

我有一個使用您的郵編搜索工廠的搜索功能。防止在控制器功能中覆蓋查詢 - codeigniter

這是基於一個問題,我今天早些時候問:How to search with the value of a cookie in php - codeigniter 我有這樣的查詢:

$匹配= $這個 - >輸入 - >後( '搜索');

這是工作正常,但當我補充一下:

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

它覆蓋第一$比賽。我怎樣才能使用這兩個搜索查詢,而不是重寫彼此?

如何設置兩個變量,如$ match1和$ match2?當我這樣做時,它只在工廠搜索而不在郵編上搜索。我只是想擁有誰擁有郵政編碼像9101例如郵政編碼和搜索工廠結果

我的模型功能:

function get_search($match, $match2) 
    { 
     $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(); 
    } 

我的控制器功能:

function searchresults() 
    { 
     $this->breadcrumbs->page = array('link'=> base_url().'home/search' ,'title' => 'Bedrijven Zoeken');    
     $this->breadcrumbs->method = array('link'=> base_url().'home/searchresults' ,'title' => 'Zoekresultaten'); 
     $data['breadcrumbs'] = $this->breadcrumbs->get(); 
     $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'); 
     $data['query'] = $this->bedrijven_model->bedrijven_tags(); 
    } 

希望有人知道我意思。

回答

1

我固定我的問題是這樣的:

我刪除了我的舊模式,而是我做這樣的:

function get_search($match, $match2) 
{ 
    if (!isset($_COOKIE['cookie'])) { 

     $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 LIKE '%".$this->input->post('cookie')."%') 

     GROUP BY `bedrijfcategorieen`.`idbedrijven`"; 
     $result = $this->db->query($query); 

     return $result->result(); 

    } 

這樣它搜索的兩件事情。位於工廠表中的郵編和設置的郵編cookie。

我的控制器:

function search() 
    { 
     $this->breadcrumbs->page = array('link'=> base_url().'home/search' ,'title' => 'Bedrijven Zoeken'); 
     $data['breadcrumbs'] = $this->breadcrumbs->get(); 
     $this->load->view('views/header'); 
     $this->load->view('views/search', $data); 
     $this->load->view('views/footer'); 
    } 

    function searchresults() 
    { 
     $this->breadcrumbs->page = array('link'=> base_url().'home/search' ,'title' => 'Bedrijven Zoeken');    
     $this->breadcrumbs->method = array('link'=> base_url().'home/searchresults' ,'title' => 'Zoekresultaten'); 
     $data['breadcrumbs'] = $this->breadcrumbs->get(); 
     $match = $this->input->post('search'); 
     if(strlen($this->input->post('cookie')) > 0){ $match2 = $this->input->post('cookie'); } else{ $match2 = '9101'; } 
     $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'); 
     $data['query'] = $this->bedrijven_model->bedrijven_tags(); 
    } 

我的觀點:

搜索:

<div style="clear: both; margin-top: 10px;"></div> 
<div class="path"><?php echo $breadcrumbs ?></div> 
<div id="bigcontent"> 
<h1>Bedrijven zoeken</h1> 
<hr> 
<br/> 
<br /> 
<p>Voer een zoekterm en postcode in om bedrijven te zoeken.</p> 
<br/> 
<form name="input" action="searchresults" method="post"> 
<input type="search" onchange="validate()" placeholder="Zoeken..." name="search" size="70"> 
<input type="search" onchange="validate()" required="true" size="14" placeholder="Postcode..." name="cookie" value="<?= $this->input->cookie('postcode', TRUE); ?>" > 

<input type="submit" value="Zoeken"> 



</form> 

<br /><br /> 

<form method="link" action="<?= base_url('bedrijven/all')?>"> 
<input type="submit" value="Alle bedrijven"> 
</form> 

</div> 

SearchResult所:

<div style="clear: both; margin-top: 10px;"></div> 
<div class="path"><?php echo $breadcrumbs ?></div> 
<div id="bigcontent"> 
<h1>Bedrijven gevonden: <?= count($query); ?></h1> 
<?= br(1); ?> 
<h2>Bedrijf:</h2> 
<?= br(1); ?> 
<hr/> 
<?php foreach($query as $item):?> 
    <a href="<?php echo base_url()?>bedrijven/<?= $item->idbedrijven?>"><h3><?= $item->Bedrijfsnaam ?></h3></a> 
    <p><b>Categorie:</b> <?= $item->Categorie ?></p> 
    <p><small><?php echo $item->Profiel ?></p> 
    <p><b><?php echo $item->Email ?></b></p> 
    <p><b>Postcode:</b> <?php echo $item->Postcode ?></p> 
    <p><b>Tags:</b></p> 
    </small> 
    <hr/> 
<?php endforeach;?> 
<br /> 
<<<a href="<?php echo base_url() ?>">Terug</a> 
</div> 
0

爲什麼不使用兩個不同的變量,如$ match1和$ match2。

+0

我試過了,但沒有奏效。比它根本不工作。 –

+0

第二個查詢仍然覆蓋第一個查詢。即使當我做\t \t \t $ match = $ this-> input-> post('search'); \t \t \t $ MATCH2 = $這個 - >輸入 - >的cookie( '郵編'); \t \t \t $數據[ '查詢'] = $這個 - > bedrijven_model-> get_search($匹配,$ MATCH2); –

+0

你是說如果你使用兩個不同的變量而不是變量都沒有收到你想要的值? –