2012-06-07 51 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; 
?>