我有一个问题,实现某些输出以回显来自Array的结果列表。 我想创建一个Live搜索引擎,通过使用AJAX在keyup函数的帮助下运行查询。Mysql结果限制输出
当表格中列出的每个匹配都会回显输出时,一切正常。 现在我想合并所有重复的条目。
的代码是这样的:
$search_term = $_POST['search_term'];
$where = "";
$search_term = preg_split('/[\s]+/', $search_term, -1, PREG_SPLIT_NO_EMPTY);
$total_search_terms = count($search_term);
$total_search_term = 0;
foreach ($search_term as $key=>$value) {
$total_search_term = $total_search_term + 1;
if ($total_search_term === 1){
if (is_numeric($value)){
$where .= "(`a` LIKE '%$value%')";
} else {
$where .= "(`b` LIKE '%$value%')";
}
}else if ($total_search_term > 1){
$where .= " AND ";
if (is_numeric($value)){
$where .= "(`a` LIKE '%$value%')";
} else {
$where .= "(`b` LIKE '%$value%')";
}
}
}
$duplicate = $db->query("SELECT a, b, COUNT(*) counter
FROM `table`
GROUP BY a, b
HAVING COUNT(*) > 1
");
$check = $duplicate->fetch_assoc();
$query = $db->query("SELECT a, b FROM table WHERE $where");
$result = $query->num_rows;
if ($result !== 0){
echo '<li id="hit">There are $result results!</li>';
while ($row = $query->fetch_assoc()) {
echo '<li>',
$row["a"],
' ',
$row["b"],
'</li>';
}
} else {
echo '<li id="hit">no result!</li>';
}
为了让输出的例子:
There are 3 results!
12345 New
12345 New
56789 Chicago
而这就是如何应该是:
There are 3 results!
12345 New (2x)
56789 Chicago
所以表:
a | b
12345 New
12345 New
56789 Chicago
非常感谢。
什么是'了''B','C'? – hsz
你好,对不起。 a是zpicode,b是城市。我更新了代码。谢谢 – bonny
什么是'$ where'? – Uby