【问题标题】:Search with two input fields in codeigniter在 codeigniter 中使用两个输入字段进行搜索
【发布时间】:2013-04-07 13:14:50
【问题描述】:

我有一个搜索表单来搜索我的数据库中的工厂。我的工厂表现在有一个名为“邮政编码”的字段,当您第一次访问该站点时,您必须输入您的邮政编码。这将保存在一个 cookie 中,因此您不必再次更改它。

现在我想使用搜索词和 cookie '邮政编码'来搜索工厂

这是我的搜索表单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

但没有适合我的答案。

【问题讨论】:

    标签: php mysql codeigniter search cookies


    【解决方案1】:

    您为$this-&gt;bedrijven_model-&gt;get_search($match, $match2); 提供了 2 个参数,但在函数本身中只提供了一个: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-&gt;input-&gt;post('search')$this-&gt;input-&gt;post('cookie')可以替换为上述sql中的$match$match2。我还建议为 cookie 设置一个默认值,因为它可以被用户从输入字段中删除;像

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

    【讨论】:

    • 它不起作用。只有 $match 有效。 $match2 不是只搜索 1 个参数
    • 如果您将邮政编码硬编码到您的函数中会发生什么,get_search($match, 'your_postcode')
    • 另外,ifcondition 可能必须移到底部;更新了我上面的代码。
    • 将尝试先将其放在底部。
    • 您可以在kees.een-site-bouwen.nl 自己查看,只需输入邮政编码并保存,然后单击蓝色搜索按钮。它不搜索邮政编码,而是搜索我表中的所有工厂。当我把它放在底部时它也不起作用
    【解决方案2】:

    如果我理解你是正确的,你为什么不在 like 语句之后添加另一个 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');
    

    【讨论】:

    • 会试试这个。它看起来正是我想要的。
    • 它只适用于一个参数。我不知道为什么。可能是我的表单有问题。
    • 查看kees.een-site-bouwen.nl,当您在对话框中设置cookie时,您可以在主页的搜索框中看到它。所以获取 cookie 不是问题。
    • 注意:我可以在 cookie 上进行搜索,但我的其他搜索字段不起作用。所以它只显示带有邮政编码的工厂。但我需要搜索 1 个工厂。带有搜索词和邮政编码。
    猜你喜欢
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多