【发布时间】:2015-06-12 02:48:56
【问题描述】:
我在网络上进行了搜索,并在 w3schools 上找到了这个链接,该链接告诉您如何使用 Php、Ajax 和 XML (link) 进行实时搜索。我可以理解他们在下面的代码上做了什么......
search.php 文件
<?php
include_once 'header.php';
?>
<html>
<head>
<script>
function showResult(str) {
if (str.length==0) {
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
</body>
</html>
还有 livesearch.php 文件
<?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 was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
但是他们接下来要做的是有一个 xml 页面 (link),其中包含他们想要搜索的所有数据,但在我的情况下,我想搜索我的数据库表并且我正在使用 SQL。我尝试进行一些编码,但我找不到如何从查询中获取数据。
links.xml 文件
<?php
error_reporting(E_ALL);
$host = "localhost";
$user = "root";
$pass = "smogi";
$database = "project";
$SQL_query = "SELECT * FROM patient WHERE fname = ???? OR lname = ????";
?>
<pages>
<link>
<title>Also display here the name of the user</title>
<url>members2.php?view=?????</url>
</link>
</pages>
凡我有 ???? 意味着我不知道该写什么。也许xml的代码需要更多的代码。
你能帮我修复我的 xml 并让它显示我的数据库中的结果吗
【问题讨论】: