用PHP AJAX和XML生活搜索PHP、AJAX、XML

2023-09-10 17:47:19 作者://嘴甜心狠办事稳

我做了网页搜索和我W3Schools的发现这个链接,告诉你如何使PHP,Ajax和XML(的链接)。我能理解他们在做他们的code什么是以下...

I did a search on web and i found on w3schools this link that tells you how to make a live search with Php, Ajax and XML (link). I can understand what they are doing on their code which is the below...

本的search.php文件

The search.php file

<?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文件

and the livesearch.php file

<?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页面(链接),它包含所有的数据,他们要搜索但对我来说我想寻找我的数据库表,我使用的SQL。我试着做一些编码,但我找不到我如何从查询中获取数据。

But what they do next is to have an xml page (link) that contains all the data they want to search but in my case I want to search my database table and I am using SQL. I tried to do some coding but I cannot find how I get the data from the query .

该links.xml文件

The links.xml file

<?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的code需要更多的code。

Wherever i have ???? means that I dont know what to write there. Maybe the code of the xml need more code.

你能不能帮我解决我的XML,使其显示从我的数据库结果

Can you help me to fix my xml and make it display results from my database

推荐答案

,你将需要编辑的文件是livesearch.php文件。 Links.xml由livesearch.php作为数据源,而你的情况是数据库中读取。修改后的livesearch.php看起来像下面这样:

The file that you would need to edit is the livesearch.php file. Links.xml is read by livesearch.php as a data source, which in your case would be the database. The modified livesearch.php would look something like the following:

<?php
$host       = "localhost";
$user       = "root";
$pass       = "Passw0rd";
$database   = "project";

$db = new PDO("mysql:host={$host};dbname={$database}", $user, $pass);
$stmt = $db->prepare("SELECT * FROM patient WHERE fname LIKE :q OR lname LIKE :q");
$stmt->bindValue(':q', '%'.$_GET['q'].'%');
$stmt->execute();

while ( $row = $stmt->fetchObject() ) {
    echo '<a href="members2.php?view=' . $row->username . '" target="_blank">' . $row->fname . ' ' . $row->lname . '</a><br/>';
}
?>

这将产生类似的输出由W3Schools的提供的livesearch.php例子。

This will produce similar output to the livesearch.php example provided by w3schools.