I have a search query for searching factories in a radius of x km around the input (postal code). It is working fine, but the problem is I can't search for the input (postal code) so when I type in 9101 for example it does not search for that postal code, only for the ones around it.
My query looks like this:
$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);
$result = $query->result_array();
return $result;
}
The $_SESSION['postcodes']
is the session with all the postcodes in a certain radius around my postcode.
I have another session called searched_post_code
this one is for the input so '9101' for example.
How can I search for factories with the filled in postal code and my filled in search term?
My searchform looks like this:
Note the large 'Search' input and the radius and postal code inputs.
I also want to match any of the searchterms with the postal code I search on.
Code for the form:
<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">
</form>
You can see it working HERE
Example of sql output:
SELECT * FROM (
bedrijfcategorieen
) JOINbedrijven
ONbedrijfcategorieen
.idbedrijven
=bedrijven
.idbedrijven
JOINcategorieen
ONbedrijfcategorieen
.idcategorieen
=categorieen
.idcategorieen
WHERE (Bedrijfsnaam
LIKE '%design%' ORPlaats
LIKE '%design%' ORTelefoonnummer
LIKE '%design%' ORWebsite
LIKE '%design%' ORProfiel
LIKE '%design%' ORAdres
LIKE '%design%' ORCategorie
LIKE '%design%') AND (Postcode REGEXP '9100|9101|9121|9103|9148|9156|9146|9122|9155|9154|9147|9125|9144|9106|9138|9113|9104|9153|') GROUP BYCategorie
,bedrijfcategorieen
.idbedrijven
Ok, I think I get what your problem is now.. What you want is to add the postcode you searched for to the $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);
I added some simple integer validation on the values in the array, you should definitely have a look at either sanitise your data or use PDO and prepared statements.
As suggested in the comments, using IN
will probably be better
$string = join(',', $postcodes);
.. AND (Postcode IN ($string)) ..