php - PHP 和 MySQL 中的实时搜索(不适用于数组?)

标签 php jquery mysql sorting while-loop

美好的一天,

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

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

我看到了这个引用文献Live Search Jquery并尝试过这是否适用于我的程序。但令我惊讶的是,它似乎只适用于预定义的值。但是当我尝试将它用于我的 table 时,该实时搜索不起作用。

太糟糕了,因为这个引用正是我真正想要实现的目标。因为它已经在搜索单词,而无需等待用户完成他/她的搜索。 大家有意见吗?

最佳答案

测试一下:

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>

关于php - PHP 和 MySQL 中的实时搜索(不适用于数组?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24797327/

相关文章:

php - 使用 session php 从 id 中选择?

java - 类型推断似乎失败了 vavr 的 Try 对 jOOQ 的 fetchOne() 函数有效

javascript - 如何在元素循环内获取元素的类名?

php - 单击按钮将 mysql 查询导出到 csv

php - 代码点火器 : insert data in database

php - Youtube API get_video_info & url_encoded_fmt_stream_map

php - 数据库 - mySQL 和 PHP 更新更新时的冲突信息

php - Wordpress 多开发人员设置

javascript - 控制 JQuery 动画函数

php - jQuery 无法处理克隆的输入字段