2015-04-07 42 views
2

我在網絡上進行了搜索,並在w3schools上找到此鏈接,告訴您如何使用Php,Ajax和XML進行實時搜索(link)。我能理解他們在做自己的代碼,這是下面有什麼...使用PHP AJAX和XML進行實時搜索

的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,使其顯示從我的數據庫結果

回答

0

,你將需要編輯的文件是livesearch.php文件。 Links.xml是由livesearch.php作爲數據源讀取的,在你的情況下它將是數據庫。修改後的livesearch.php看起來如下所示:

<?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示例類似的輸出。

+0

所以,如果我明白你說我只需要search.php和livesearch.php文件。用你的代碼更改livesearch.php文件中的所有代碼。對? 如果我做你所說的我得到這個錯誤致命錯誤:帶有消息'SQLSTATE [HY000] [1044]的未捕獲的異常'PDOException'在E:中數據庫'項目'的用戶'''localhost' \ xampp \ htdocs \ ptixiaki \ livesearch.php:7堆棧跟蹤:#0 E:\ xampp \ htdocs \ ptixiaki \ livesearch.php(7):PDO - > __ construct('mysql:host = loca ...')# 1 {main}拋出E:\ xampp \ htdocs \ ptixiaki \ livesearch.php第7行' – DogFace

+0

PDO構造函數需要一個有效的'dsn,用戶名,密碼'。所以在你的情況下,'$ db = new PDO(「mysql:host = {$ host}; dbname = {$ database}」,$ user,$ pass);'。閱讀手冊將會幫助你很多! http://php.net/pdo :) – tftd

+0

@tftd首先感謝你的時間。我做了你所說的,現在我得到了這個致命錯誤:無法通過參考在第9行的E:\ xampp \ htdocs \ ptixiaki \ livesearch.php中引用參數2,第9行是這一個'$ stmt-> bindParam (':q','%'。$ _ GET ['q']。'%');' – DogFace