在codeigniter中使用两个输入字段进行搜索

时间:2013-04-15 12:16:12

标签: php mysql codeigniter search cookies

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

现在我想使用searchterm和cookie'postcode'

搜索工厂

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

但没有答案对我有用。

2 个答案:

答案 0 :(得分:1)

您为$this->bedrijven_model->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);
在上面的sql中,

$this->input->post('search')$this->input->post('cookie')可以替换为$match$match2。我还建议为cookie设置默认值,因为它可以由用户从输入字段中删除;

之类的东西
$match2 = $this->input->post('cookie') ? $this->input->post('cookie') : 'default';

答案 1 :(得分:0)

如果我理解你是正确的,为什么不在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);

$这 - &GT; DB-化合物其中( “邮编”,$邮编);

    $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');
相关问题