我已经实现了搜索,其工作是从数据库中找出属性记录。搜索在某种程度上完全取决于用户输入的关键字。
认为,
如果用户键入关键字 like1234
或它应该从数据库的列中4775
搜索773
记录。zip
接下来,如果用户键入关键字 like 6 S Surfway
or70 N Grove St
或它应该只从数据库中1661-114 Old Country Rd
搜索列中的记录。address
接下来,如果用户键入关键字 like Calverton
orFreeport
或它应该只从数据库中Riverhead
搜索列中的记录。town
所以,我的问题是我无法根据用户输入的关键字过滤这些记录。我的问题是从所需的列中过滤基于关键字的记录。
这是我正在使用的代码:
<?php
header('Content-Type: application/json');
$key = $_REQUEST['keyword'];
$searchOp = $_REQUEST['searchOp'];
if($searchOp==1 || $searchOp==6)
{
$searchChk = "(`property_chk` = '1' OR `property_chk` = '6')";
}else{
$searchChk = "(`property_chk` = '$searchOp')";
}
$data = array(
'locations' => array(),
'errors'=>array(),
'success'=> true,
);
if($db->connect_errno > 0){
$data['errors'][] = $db->connect_error;
die(json_encode($data));
}
$index = 0;
if(intval($key) >= 3){
$zipQuery = $db->query("SELECT zip FROM tbl_property WHERE zip like '%$key%' AND $searchChk GROUP BY zip LIMIT 0,5");
if($zipQuery->num_rows>0){
while($row = $zipQuery->fetch_assoc()){
$data['locations'][] = array(
"value"=> $row['zip'],
"altValue"=> null,
"display"=> $row['zip'],
"type"=> "zip",
"propertyIndex"=> "",
"index"=> $index
);
$index = $index + 1;
}
}
}elseif (!intval($key) && count($key) > 4){
$cityQuery = $db->query("SELECT ste,town FROM tbl_property WHERE town like '%$key%' AND $searchChk GROUP BY town LIMIT 0,5");
if($cityQuery->num_rows>0){
while($row = $cityQuery->fetch_assoc()){
$data['locations'][] = array(
"value"=> $row['town'].', '.$row['ste'],
"altValue"=> null,
"display"=> $row['town'].', '.$row['ste'],
"type"=> "city",
"propertyIndex"=> "",
"index"=> $index
);
$index = $index + 1;
}
}
}elseif (count($key) > 1){
$addrQuery = $db->query("SELECT addr FROM tbl_property WHERE addr like '%$key%' AND $searchChk GROUP BY addr LIMIT 0,5");
if($addrQuery->num_rows>0){
while($row = $addrQuery->fetch_assoc()){
$data['locations'][] = array(
"value"=> $row['addr'],
"altValue"=> null,
"display"=> $row['addr'],
"type"=> "address",
"propertyIndex"=> "",
"index"=> $index
);
$index = $index + 1;
}
}
}
$db->close();
header('Content-Type: application/json');
echo json_encode($data);