【问题标题】:php mysql ajax live search arrayphp mysql ajax 实时搜索数组
【发布时间】:2012-06-11 14:14:24
【问题描述】:

我有一个 ajax 实时搜索脚本工作,只是它似乎只返回数组的第一行。不知道这是否与我的循环有关,但是,如果有人能发现错误,将不胜感激,因为我似乎找不到它。我不包括 javascript,因为我很确定这不是错误,因为 php 文件正在触发。它只是呼应第一个打击,而不是迭代其他打击。

//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;

【问题讨论】:

  • 如果您试图回显多个提示,那么$hint 变量将在循环的每次迭代中被覆盖?尝试改用.=,这会将值连接在一起。

标签: php mysql ajax arrays


【解决方案1】:

您将在每次 while 循环迭代时覆盖您的 $hint 变量。尝试在while 之前声明它(删除你现在拥有它的位置)

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

【讨论】:

    【解决方案2】:

    尝试使用这种方法:

    <?
    $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;
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 2016-05-09
      • 2012-06-08
      • 1970-01-01
      • 2015-04-11
      • 2016-10-16
      • 1970-01-01
      相关资源
      最近更新 更多