1

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

现在我想使用搜索词和 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();
   }

但这不起作用。我在这里问了一个类似的问题:防止在控制器功能中覆盖查询 - codeigniter

但没有对我有用的答案。

4

2 回答 2

1

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

$match2 = $this->input->post('cookie') ? $this->input->post('cookie') : 'default';
于 2013-04-15T12:29:09.397 回答
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);

$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');
于 2013-04-15T12:27:28.877 回答