我在使用AJAX和PHP创建多个字段实时搜索引擎时遇到了问题。
到目前为止,不需要搜索多个字段,所以我有一个简单的查询,它只对一个字段很好地工作。
由于使用onkeyup函数进行实时搜索,我发现了一些问题。我要解释的第一个问题是:
表的结构非常简单:zipcode | city
例如,有人输入了12345;当然,这将是邮政编码。但是,如果有人进入12345 hometown,那么第一个关键字是邮政编码,其次是城市呢?
关键字将通过使用preg_split('/[\s]+/', $search_term)进行拆分,因此,我将收到一个带有将被搜索的单个关键字的数组。在上面的例子中,应该是:key[0] => 12345和key[1] => hometown。
我使用的查询如下:
$where = "";
$search_term = preg_split('/[\s]+/', $search_term); //splits the whole string into single keywords
$total_search_terms = count($search_term); //counts the array-keys from $search_term
foreach ($search_term as $key=>$single_term) {
$where .= "`zipcode` LIKE '$single_term%' OR `city` LIKE '$single_term%' ";
if ($key != ($total_search_terms - 1)){ //adds AND to the query in case in case of a non empty array-key
$where .= " AND ";
}
}
$query = $db->query("SELECT COUNT(*) FROM table WHERE $where");
...好吧,到目前为止还不错。现在的问题是关键字可以一次又一次匹配每个字段。
给出进一步的例子:
如果从上面输入12345 hometown,这意味着key[0] => 12345可以匹配字段zipcode或city。此条件与key[1] => hometown相同,甚至可以与字段zipcode或city相匹配。所以,即使进入相反的方向:hometown 12345的意思也是一样的。
这是我遇到的第一个问题。
我正在寻找一个逻辑来构造查询。因此,如果进入12345 hometown,我希望有这样的东西:
当key[0] => 12345匹配字段时,zipcode不检查key[1] => hometown是否在zipcode或city中匹配,因为key[0]已经在zipcode中匹配,所以key[1]需要是city是非常合乎逻辑的。
更新
好的,为了告诉我的第二个问题,我想让你们看看david strachan的答案,他提到,当城市包含多个字符串时,就会产生一个问题。假设搜索字符串如下所示:
12345 New York关键是:
key[0] => 12345, key[1] => New, key[2] => York好的,现在的问题是,我可以检查其中一个键是否包含整数,如果字符串长度正好是5,我知道它就是邮政编码。
key[0] => 12345 //if ( stringlen(key[0|) === 5) === true && is_int(key[0]) === true) {zipcode}到目前为止还不错,但真正的问题是城市背后的逻辑。我最初的想法是,我可以说,所有不包含整数的键都必须是城市,所以我可以将它们转换为一个键。
key[1] => New, key[2] => York //if ( !is_int(key[1|) === true && !is_int(key[2|) === true) {$create_one_key = array_fill_keys(key[1], key[1]+key[2]);}好吧,但是万一将来我想加街名呢?我不知道如何区分和测试街道名称,甚至是城市名称。
发布于 2013-04-16 20:49:01
另一种方法是使用JQuery Autocomplete搜索MySQL数据库。下面的代码使用PDO。
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script type="text/javascript">
jQuery(document).ready(function(){
$('#zipsearch').autocomplete({source:'suggest.php', minLength:2});
});
</script>
<style type="text/css">
li.ui-menu-item { font-size:10px}
</style>
</head>
<body>
<h2>jQuery UI Autocomplete - With MySQL </h2>
<form onsubmit="return false;">
Search:
<input id="zipsearch" type="text" size ="60"/>
</form>suggest.php
require("dbinfo.php");//db connection strings
// if the 'term' variable is not sent with the request, exit
if ( !isset($_REQUEST['term']) )
exit;
$term = $_REQUEST['term'];
if (is_numeric($term)){
$query = "SELECT * from ziptest WHERE zip LIKE ? OR address LIKE ? LIMIT 0,10";
}else{
$query = "SELECT * from ziptest WHERE city LIKE ? OR state LIKE ? LIMIT 0,10";
}
$term.="%";
// connect to the database
try {
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Prepare statement
$stmt = $dbh->prepare($query);
// Assign parameters
$stmt->bindParam(1,$term);
$stmt->bindParam(2,$term);
// setting the fetch mode
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$data = array();
while($row = $stmt->fetch()) {
$data[] = array(
'label' => $row['address'] .', '. $row['city'] .', '. $row['state'] .', '.$row['zip'] ,
'value' => "ID ".$row['id'] .': '.$row['address'] .', '. $row['city'] .', '. $row['state'] .', '.$row['zip']
);
}
echo json_encode($data);
}
catch(PDOException $e) {
echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing
file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]'). $e->getMessage()."\r\n", FILE_APPEND);
}
// close the connection
$dbh = null;DEMO
发布于 2013-04-14 16:05:40
通过添加0来检查$search_term[0]是string(city)还是int(zipcode),然后相应地格式化查询。
$search_term = '1234 paisley';
$search_term = preg_split('/[\s]+/', $search_term); //splits the whole string into single keywords
$total_search_terms = count($search_term); //counts the array-keys from $search_term
$test = $search_term[0]+0;//If string returns 0
if($total_search_terms == 1){
if ($test == 0 ){
$where = "WHERE `city` LIKE `%$search_term[0]%`";
}else{
$where = "WHERE `zipcode` LIKE `%$search_term[0]%` ";
}
}else{
if ($test == 0 ){
$where = "WHERE `zipcode` LIKE `%$search_term[1]%` AND `city` LIKE `%$search_term[0]%`";
}else{
$where = "WHERE `zipcode` LIKE `%$search_term[0]%` AND `city` LIKE `%$search_term[1]%`";
}
}
$query = "SELECT COUNT(*) FROM table $where";
echo $query;一个问题是你如何对待纽约。我将留给你如何解决这个问题。
编辑
纽约$total_search_terms > 2
https://stackoverflow.com/questions/15997717
复制相似问题