我有一个搜索查询,用于搜索输入周围半径x km的工厂(邮政编码)。它工作正常,但问题是我无法搜索输入(邮政编码)所以当我输入9101例如它不搜索该邮政编码时,只针对它周围的邮政编码。
我的查询如下:
$string = implode($_SESSION['postcodes'], '|');
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 REGEXP '$string')
GROUP BY `Categorie`, `bedrijfcategorieen`.`idbedrijven`";
$query = $this->db->query($query);
echo '<pre>';
echo '</pre>';
$result = $query->result_array();
return $result;
$_SESSION['postcodes']
是包含邮政编码周围某个半径内所有邮政编码的会话。
我有另一个名为searched_post_code
的会话,这个会话用于输入,例如'9101'。
如何使用填写的邮政编码和填写的搜索字词来搜索工厂?
我的搜索表格如下:
请注意较大的“搜索”输入以及半径和邮政编码输入。
我还希望将任何搜索字符与我搜索的邮政编码匹配。
表单代码:
<form name="input" method="post" action="searchresults" class="pro6pp_range">
<input type="search" onchange="validate()" placeholder="Zoeken..." name="search" size="70">
<select class="range">
<option value="5" selected="selected">5 km</option>
<option value="10">10 km</option>
<option value="15">15 km</option>
<option value="20">20 km</option>
<option value="25">25 km</option>
</select>
<input type="search" name="searchpc" class="postcode" value="<?= $this->input->cookie('postcode'); ?>" placeholder="Postcode (1234)" maxlength="4">
<input type="submit" value="Zoeken">
我希望它有点清楚:
您可以看到它有效HERE
sql输出示例:
SELECT * FROM(
bedrijfcategorieen
)加入bedrijven
ONbedrijfcategorieen
。idbedrijven
=bedrijven
。idbedrijven
加入categorieen
bedrijfcategorieen
。idcategorieen
=categorieen
。idcategorieen
WHERE(Bedrijfsnaam
LIKE'%design%'或Plaats
LIKE'%design%'或Telefoonnummer
LIKE'%design%'或Website
LIKE'%design%'或Profiel
LIKE '%design%'或Adres
LIKE'%design%'或Categorie
LIKE'%design%') AND(邮政编码REGEXP '9100 | 9101 | 9121 | 9103 | 9148 | 9156 | 9146 | 9122 | 9155 | 9154 | 9147 | 9125 | 9144 | 9106 | 9138 | 9113 | 9104 | 9153 |') GROUP BYCategorie
,bedrijfcategorieen
。idbedrijven
答案 0 :(得分:2)
好的,我想我现在得到了你的问题..你想要的是将搜索到的邮政编码添加到$string
$postcodes = (is_array($_SESSION['postcodes']) ? $_SESSION['postcodes'] : array());
$postcodes[] = $_SESSION['searched_post_code'];
$postcodes = array_filter(filter_var_array($postcodes, FILTER_VALIDATE_INT));
$string = join('|', $postcodes);
我在数组中的值上添加了一些简单的整数验证,您一定要查看清理数据或使用PDO和预处理语句。
正如评论中所建议的,使用IN
可能会更好
$string = join(',', $postcodes);
.. AND (Postcode IN ($string)) ..