php - 使用多个单词进行实时 PHP Ajax 搜索
问题描述
编辑
我能够以一种我希望的不那么花哨的方式解决这个问题——但它给出了正确的答案——除了我希望很快解决的非常小的和罕见的不一致。如果您知道如何使它变得更好,请在下面发布您对此问题的答案。:)
我是怎么做到的
答案很简单。explode
使用并测试每个部分的标题和名称,将搜索字符串拆分为所有部分。
try{
//$this->string is the string provided by the $_POST["string"]
$strings = explode(" ",$this->string);
$sql = "SELECT sithname,
(SELECT label FROM titles WHERE id = m.title ) as titlename,
(SELECT label FROM departments WHERE id = m.department ) as departmentname
FROM members m
WHERE (SELECT label FROM titles WHERE id = m.title ) LIKE :string OR sithname LIKE :string
ORDER BY joined DESC";
$stmt = $this->conn->prepare($sql);
//Try every part of the string
foreach($strings as $key => $value){
//This is the actual trick
//Using LIKE + the combination of value+"%" can get you the full name, if you only type in the first letter for instance
$stmt->execute(array(":string"=>$value.'%'));
//If there are any rows found, save them to the $data array
if($stmt->rowCount() > 0){
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
//Just returning the data for Ajax
if(isset($data)){
if($data != NULL){
echo json_encode(array("code"=>0,"param"=>$data,"msg"=>"Matches found"));
}else{
echo json_encode(array("code"=>5,"param"=>"","msg"=>"No match found"));
}
}else{
echo json_encode(array("code"=>5,"param"=>"","msg"=>"No match found"));
}
}
catch(PDOException $e){
echo json_encode(array("code"=>4,"param"=>"","msg"=>$e->getCode()));
}
原帖
您好,最近我遇到了一个问题,无法解决这个问题。也许你们中的一些人有解决方案或冲动。
我想要达到的目标
就像在 Facebook 中一样 - 一种搜索表单,用户可以使用他们的“姓名”、“职务”或“职务”和“姓名”的混合来搜索其他用户。例如:
SQL 数据:
+-------------+-----------+
| name | titlename |
+-------------+-----------+
| Seriuna Tar | Darth |
+-------------+-----------+
| Taelax | Darth |
+-------------+-----------+
| Thar | Adept |
+-------------+-----------+
如果用户仅输入“Darth”作为输入
结果:
+--------------+-----------+
| name | titlename |
+--------------+-----------+
| Seriuna Tar | Darth |
+--------------+-----------+
| Taelax | Darth |
+--------------+-----------+
如果用户输入“Darth Seriuna T”作为输入
结果:
+--------------+-----------+
| name | titlename |
+--------------+-----------+
| Seriuna Tar | Darth |
+--------------+-----------+
!!请注意,名称中可以有空格!!
因为这显然是不够的......它必须发生LIVE。
因此,当您键入时,结果已经显示。
到目前为止我所拥有的
我有一个单一的文本输入。
<input id="search-bar" class="form-control mr-0 ml-0 w-100" type="text" placeholder="Search" aria-label="Search" autocomplete="off" spellcheck="false">
通过 JQuery,我检查文本输入中的变化。(这不是全部代码,只是相关部分)
$("#search-bar").keyup(function() {
//Check if there is valid text being typed
if(searchtext.trim() != ""){
//Set data that is going to be sent
var formData = {
'string' : $('#search-bar').val().trim()
};
$.ajax({
type: 'post',
url: 'php/holobook/search.php',
data: formData,
dataType : 'json',
encode : true,
success: function (data) {
//Clear all previous findings
$('.result-list').empty();
//Go through each person found
$.each( data["param"], function( key, value ) {
//Prepare a div with the userdata
let result = '<a class="btn btn-flat d-block text-left ml-2 p-2" href="/holobook/u/'+value["sithname"]+'">'+value["titlename"]+' '+value["sithname"]+' <small class="text-muted ml-2"> - Dark Council</small></a>';
//Append it to the result-list container
$('.result-list').append(result);
});
//Show the result box (like in Facebook)
$('.result-container').show();
},
error: function (jqXHR, textStatus, errorThrown)
{
console.log(jqXHR); //Errors
}
});
}else{
//If input is empty
$('.result-container').hide();
$('.result-list').empty();
}
});
乐趣开始的地方
当然,我不能只将整个用户列表还给客户端。过滤/搜索需要在服务器上进行。
我尝试仅使用 SQL 语句来解决它
SELECT sithname,
(SELECT label FROM titles WHERE id = m.title ) as titlename
FROM members m
WHERE sithname LIKE :str OR titlename LIKE :str
ORDER BY joined DESC
这当然是失败的,因为它仅在输入标题或名称时才有效。
我的下一步是 - 正如我一直计划的那样 - 使用 PHP 来“过滤”结果。
我得到的数据如下(json 编码):
array(11) {
[0]=>
array(3) {
["sithname"]=>
string(6) "Taelax"
["titlename"]=>
string(7) "Darth"
}
[1]=>
array(3) {
["sithname"]=>
string(15) "Seriuna Tar"
["titlename"]=>
string(7) "Darth"
}
}
因此,要通过 foreach 访问它,您需要
foreach($results as $key => $value){
//code
}
那么你知道我怎样才能得到我需要的数据吗?似乎我必须将单词分成单个字母,然后使用它们的位置来检查匹配项?
非常感谢您抽出宝贵时间 - 如果您有任何想法,我对每一个小提示都很高兴。
解决方案
推荐阅读
- rust - Rust tokio_tls - 接受连接时出错
- sql - 数值数据类型和小数
- wordpress - Elementor Page Builder Plugin 是否适用于多站点 wordpress?
- json - 如何访问 select 语句中的特定 json 字段以及 count 函数?
- typescript - node_modules/@types/node d.ts 文件出现问题
- ios - 如何快速找到 UIButton 的正确位置?
- azure-devops - 使用 Azure DevOps 作为数据源的 Power BI 报告停止运行
- jquery - Ajax 请求到达控制器 laravel 时为空
- bash - 无法从终端运行 PyPi 可读性包
- opc - 启动UA-ModelCompiler时出现问题