实时搜索在PHP和MySQL(不工作在数组?)


Live Search in PHP and MySQL (not working in array?)

大家好,

我有一个页面,其中从数据库中获取的数据列表正在一个数组中获取。下面是代码:

<table width="100%">
<thead>
<tr>
<th>Name:</th>
<th>Address:</th>
<th>Birthday:</th>
</tr>
</thead>
$samp=mysql_query("select * from client order by id asc");
$counter=0;
while($row=mysql_fetch_array($samp))
    {
    $id         =   $row['id'];
    $name       =   $row['name'];
    $address    =   $row['address'];
    $birthday   =   $row['birthday'];

        if($counter%2)
        {
        ?>
        <tbody>
        <tr id="<?php echo $id; ?>">
        <?php } else { ?>
        <tr id="<?php echo $id; ?>">
        <?php } ?>
        <td>
        <span class="text"><?php echo $id.".  ".$name; ?></span>
        </td>
        <td>
        <span class="text"><?php echo $address; ?></span> 
        </td>
        <td>
        <span class="text"><?php echo $birthday; ?></span> 
        </td>
        <tr>
       <!--and so on -->
        </tbody>

我看到了这个参考实时搜索Jquery,并尝试如果这将与我的程序工作。但令我惊讶的是,它似乎只适用于预定义的值。但是当我试着把它用在我的表上时,这个实时搜索不起作用。

太糟糕了,因为这个参考是我真正想要实现的。因为它已经在搜索单词,而没有等待用户完成他/她的搜索。有人有意见吗?

测试:

index . html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
function showResult(str)
{
if (str.length==0)
  {
  document.getElementById("livesearch").innerHTML="";
  return;
  }
  xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
<title>main page</title></head>
<body>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
</body>
</html>

livesearch.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
      for($i=0; $i<($x->length); $i++)
        {
        $y=$x->item($i)->getElementsByTagName('title');
        $z=$x->item($i)->getElementsByTagName('url');
              if ($y->item(0)->nodeType==1)
                    {
                //find a link matching the search text
                      if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
                            {
                                if ($hint=="")
                                    {
                                      $hint="<a href='" .
                                      $z->item(0)->childNodes->item(0)->nodeValue .
                                      "' target='_blank'>" .
                                      $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
                                    }
                                else
                                    {
                                      $hint=$hint . "<br /><a href='" .
                                      $z->item(0)->childNodes->item(0)->nodeValue .
                                      "' target='_blank'>" .
                                      $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
                                    }
                            }
                    }
        }
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
  {
        $response="no suggestion";
  }
else
  {
        $response=$hint;
  }
//output the response
echo $response;
?> 
</body>
</html>

links.xml

<?xml version="1.0" encoding="utf-8"?>
<links>
    <link>
        <title>about</title>
        <url>main.html</url>
    </link>
    <link>
        <title>Gmail</title>
        <url>www.google.com</url>
    </link>
    <link>
        <title>yahoo</title>
        <url>www.google.com</url>
    </link>
    <link>
        <title>bing</title>
        <url>www.google.com</url>
    </link>
    <link>
        <title>torrent search</title>
        <url>www.google.com</url>
    </link>
    <link>
        <title>Zend</title>
        <url>www.google.com</url>
    </link>
</links>