我想在PHP中进行实时搜索。我使用CodeIgniter。我遇到了可能匹配的记录问题。他们不返回,例如,当我输入A时,我有两个名字亚当和安德鲁我只会看到亚当我必须写安得到安德鲁。从基地退回选择只有一行,而不是所有可能匹配
代码模型:
public function get_user_search($q){
$query = $this->db->like('name', $q, 'after');
$query = $this->db->get('book');
$result = $query ->row_array();
echo json_encode($result);
}
代码演示:
function showResult(str){
if (str.length==0){
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px";
return;
}
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{ // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var obj = JSON.parse(xmlhttp.responseText);
if(obj!=null){
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key + " -> " + obj[key]); //here I see only one row from base
}
}
//console.log(JSON.stringify(obj));
// document.getElementById("livesearch").innerHTML = obj.name+" "+obj.surname+" "+obj.telephone+" "+obj.street;
}else{
document.getElementById("livesearch").innerHTML = "brak wynikow";// have not found match - information
}
}
}
xmlhttp.open("GET","search?q="+str,true);
xmlhttp.send();
}
使用
result_array();
不要用你的问题的链接。在这里添加你的代码。要将其格式化为代码,请在每行之前添加四个空格或制表符。 –