在PHP和MySQL中进行实时搜索(在数组中不工作?)
你好 我有一个页面,从数据库中获取的数据列表正在数组中提取。代码如下:在PHP和MySQL中进行实时搜索(在数组中不工作?),php,jquery,mysql,sorting,while-loop,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
<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>
姓名:
地址:
生日:
$samp=mysql_查询(“按id asc从客户订单中选择*);
$counter=0;
while($row=mysql\u fetch\u数组($samp))
{
$id=$row['id'];
$name=$row['name'];
$address=$row['address'];
$birthday=$row['birthday'];
如果($counter%2)
{
?>
测试它:
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>
函数显示结果(str)
{
如果(str.length==0)
{
document.getElementById(“livesearch”).innerHTML=“”;
返回;
}
xmlhttp=新的XMLHttpRequest();
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“livesearch”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“livesearch.php?q=“+str,true”);
xmlhttp.send();
}
主页
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”);
$x=$xmlDoc->getElementsByTagName('link');
//从URL获取q参数
$q=$_GET[“q”];
//如果q的长度>0,则从xml文件中查找所有链接
如果(斯特伦($q)>0)
{
$hint=“”;
对于($i=0;$i长度);$i++)
{
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
如果($y->item(0)->nodeType==1)
{
//查找与搜索文本匹配的链接
if(stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
{
如果($hint==“”)
{
$hint=“”;
}
其他的
{
$hint=$hint.“
”;
}
}
}
}
}
//如果未找到提示,则将输出设置为“无建议”
//或设置为正确的值
如果($hint==“”)
{
$response=“无建议”;
}
其他的
{
$response=$hint;
}
//输出响应
回音$应答;
?>
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>
关于
main.html
Gmail
www.google.com
雅虎
www.google.com
宾
www.google.com
种子搜索
www.google.com
泽德
www.google.com
我很难理解你的问题。你到底想达到什么目的?代码在哪里没有达到你想要的目的?你期望发生什么?实际发生了什么?@cam我想做的是创建一个搜索框,让用户在其中键入他/她的搜索(甚至只是一封信)将触发搜索函数并调用用户一直从数据库中查找的字段。就像我上面发布的链接一样。但是当我尝试使用它时,它不起作用。所以我想可能是因为我在数据库中的列上放置的值。因为代码似乎只对预定义值起作用。您可以使用mysql syn然后在livesearch.php中返回该值。