我能够以我所希望的不太花哨的方式解决问题-但它给出了正确的答案-除了极小的和罕见的不一致之处,我希望尽快解决。如果您有一个更好的想法,请在下面发布对这个问题的答案。 :)
我是怎么做到的
答案很简单。使用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
}
那么您是否知道如何获取所需的数据?似乎我必须将单词拆分成单个字母,然后使用其位置检查匹配项?
非常感谢您抽出宝贵的时间-如果您有任何想法,我对每一个小提示都很高兴。