doumaji6215 2015-04-07 13:23
浏览 75
已采纳

使用PHP AJAX和XML进行实时搜索

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

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> 

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

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 .

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>

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

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

  • 写回答

1条回答 默认 最新

  • dongli4711 2015-04-07 14:22
    关注

    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/>';
    }
    ?>
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?