php - Mysql search with 2 times AND on the same field -
i have search query searching factories in radius of x km around input (postal code). working fine, problem can't search input (postal code) when type in 9101 example not search postal code, ones around it.
my query looks this:
$string = implode($_session['postcodes'], '|'); if (!isset($_cookie['cookie'])) { $query = "select * (`bedrijfcategorieen`) join `bedrijven` on `bedrijfcategorieen`.`idbedrijven` = `bedrijven`.`idbedrijven` join `categorieen` on `bedrijfcategorieen`.`idcategorieen` = `categorieen`.`idcategorieen` (`bedrijfsnaam` '%".$this->input->post('search')."%' or `plaats` '%".$this->input->post('search')."%' or `telefoonnummer` '%".$this->input->post('search')."%' or `email` '%".$this->input->post('search')."%' or `website` '%".$this->input->post('search')."%' or `profiel` '%".$this->input->post('search')."%' or `adres` '%".$this->input->post('search')."%' or `categorie` '%".$this->input->post('search')."%') , (postcode regexp '$string') group `categorie`, `bedrijfcategorieen`.`idbedrijven`"; $query = $this->db->query($query); echo '<pre>'; echo '</pre>'; $result = $query->result_array(); return $result;
the $_session['postcodes']
session postcodes in radius around postcode. have session called searched_post_code
1 input '9101' example.
how can search factories filled in postal code , filled in search term?
my searchform looks this:
note large 'search' input , radius , postal code inputs.
i want match of searchterms postal code search on.
code 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">
i hope it's bit clear:
you can see working here
example of sql output:
select * (
bedrijfcategorieen
) joinbedrijven
onbedrijfcategorieen
.idbedrijven
=bedrijven
.idbedrijven
joincategorieen
onbedrijfcategorieen
.idcategorieen
=categorieen
.idcategorieen
(bedrijfsnaam
'%design%' orplaats
'%design%' ortelefoonnummer
'%design%' orwebsite
'%design%' orprofiel
'%design%' oradres
'%design%' orcategorie
'%design%') , (postcode regexp '9100|9101|9121|9103|9148|9156|9146|9122|9155|9154|9147|9125|9144|9106|9138|9113|9104|9153|') groupcategorie
,bedrijfcategorieen
.idbedrijven
ok, think problem now.. want add postcode searched $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 simple integer validation on values in array, should have @ either sanitise data or use pdo , prepared statements.
as suggested in comments, using in
better
$string = join(',', $postcodes); .. , (postcode in ($string)) ..
Comments
Post a Comment