1

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

这是基于我今天早些时候提出的一个问题:如何在 php 中使用 cookie 的值进行搜索 - codeigniter 我有一个这样的查询:

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

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

$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();
    }

希望有人知道我的意思。

4

2 回答 2

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();

  }

这样它会搜索两件事。位于 factory 表中的邮政编码和设置的邮政编码 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>

搜索结果:

<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>
于 2013-05-01T10:05:16.300 回答
0

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

于 2013-04-15T09:29:33.287 回答