I am trying to build a search box which will search in 2 columns.
html
<div class="autocomplete-list1">
<input type="text" class="searchpropertyinputs areaname-list-completed" name="buildingorlocation" id="buildingorlocation" placeholder="Building or Location" onkeyup="autofillbuildingorlocation()" maxlength="50" />
<ul class="areaname-list searchpropertyinputs" id="property_buildingorlocation_list"></ul>
</div>
php
$property_buildingorlocation = ($_GET['buildingorlocation']);
$property_buildingorlocation = str_replace(',', ' ', $property_buildingorlocation);
$select = $con->prepare(
"SELECT
*
from
tbl_property
WHERE
property_buildingname LIKE '%$property_buildingorlocation%'
OR property_areaname LIKE '%$property_buildingorlocation%'");
$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();
If i enter up to two key words like madison garden, then the result comes but if i put three like madison garden road then there is no result... can anyone enlighten me please..
Try the below code:
<?php
$property_buildingorlocations = array_filter(explode(',', $_GET['buildingorlocation']));
$query = "SELECT * from tbl_property ";
$count = count($property_buildingorlocations);
if($count){
$query .=" WHERE ";
}
$i = 0;
foreach ($property_buildingorlocations as $property_buildingorlocation){
$query .= "property_buildingname LIKE '%".trim($property_buildingorlocation)."%'";
if(++$i !== $count) {
$query .=" OR ";
}
}
$select = $con->prepare($query);
$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();