【发布时间】:2015-05-21 01:50:11
【问题描述】:
我已经实现了搜索,其工作是从数据库中找出属性记录。搜索在某种程度上完全取决于用户输入的关键字。
假设,
如果用户键入 1234 或 4775 或 773 之类的关键字,则应从数据库中的 zip 列中搜索记录。
接下来,如果用户键入 6 S Surfway 或 70 N Grove St 或 1661-114 Old Country Rd 之类的关键字,则应仅从数据库中搜索 address 列中的记录。
接下来,如果用户键入 Calverton 或 Freeport 或 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);
【问题讨论】:
标签: php validation mysqli filtering keyword-search