我想在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();
不要用你的問題的鏈接。在這裏添加你的代碼。要將其格式化爲代碼,請在每行之前添加四個空格或製表符。 –