防止在控制器功能中重写查询 - codeigniter

时间:2013-04-15 09:00:54

标签: codeigniter search cookies

我有一个搜索功能,用于使用您的邮政编码搜索工厂。

这是基于我今天早些时候提出的一个问题:How to search with the value of a cookie in php - codeigniter 我有这样的查询:

  

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

这工作正常,但是当我添加:

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

它超越了第一场$ match。如何在不重写彼此的情况下使用两个搜索查询?

如何设置$ 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();
    }

希望有人知道我的意思。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

为什么不使用两个不同的变量,例如$ match1和$ match2。