2012-06-07 49 views
0

我有一個ajax實時搜索腳本,除了它似乎只返回數組的第一行。不知道這是否與我的循環,但是,如果任何人都可以發現錯誤,將不勝感激幫助,因爲我似乎無法找到它。我不包括JavaScript因爲我很確定這不是錯誤,因爲PHP文件正在解僱。這只是迴應第一次打擊而不是重複其他人。php mysql ajax實時搜索陣列

//run query on dbase then use mysql_fetch_array to place in array form 
while($a = mysql_fetch_array($res,MYSQL_BOTH)) 
{ 
    //lookup all hints from array if length of q>0 
    if (strlen($q) > 0) 
    { 
     $hint=""; 
     for($i=0; $i<count($a); $i++) 
     { 
      if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) 
      { 
       $n = $a['first']." ".$a['last']; 
       if ($hint=="") 
       { 
        $hint='<a href="mailto.php">'.$n.'</a>'; 
       } 
       else 
       { 
        $hint=$hint.'<br><a href="mailto.php">'.$n.'</a>';// we do not seem to get here 
       } 
      } 
     } 
    } 

// Set output to "no suggestion" if no hints were found 
// or to the correct values 
}//close while fetch 
echo $hint; 
+0

將在循環的每次迭代中被覆蓋?嘗試使用'。='來代替,這會將值連接在一起。 – martincarlin87

回答

2

你在每個while循環迭代改寫你的$hint變量。嘗試while之前宣佈它(刪除,你有現在)

$hint = ""; 
while($a = mysql_fetch_array($res,MYSQL_BOTH)) 
{ 
+0

工作!非常感謝 – user1260310

1

嘗試使用此方法:如果你想呼應不止一個提示,則`$ hint`變量

<? 
$output = ''; 
while($a = mysql_fetch_array($res,MYSQL_BOTH)) { 
    if (strlen($q) > 0) { 
     for($i=0; $i<count($a); $i++) { 
      if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) { 
       $n = $a['first']." ".$a['last']; 
       if ($output == "") { 
        $output = '<a href="mailto.php">'.$n.'</a>'; 
       } else { 
        $output .= '<br><a href="mailto.php">'.$n.'</a>'; 
       } 
      } 
     } 
    } 
} 
echo $output; 
?>