1

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

Waaaaat
  • 634
  • 3
  • 14
  • 29

1 Answers1

0

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.

Waaaaat
  • 634
  • 3
  • 14
  • 29
thorne51
  • 588
  • 7
  • 23
  • So if I understand you said that I need only the search.php and the livesearch.php files . And change all the code from the livesearch.php file with yours code . Right? if i do what you said i get this error `Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'project'' in E:\xampp\htdocs\ptixiaki\livesearch.php:7 Stack trace: #0 E:\xampp\htdocs\ptixiaki\livesearch.php(7): PDO->__construct('mysql:host=loca...') #1 {main} thrown in E:\xampp\htdocs\ptixiaki\livesearch.php on line 7` – Waaaaat Apr 07 '15 at 15:10
  • The PDO constructor requires a valid `dsn,username,password`. So in your case, `$db = new PDO("mysql:host={$host};dbname={$database}", $user, $pass);`. Reading the manual will help you a lot! http://php.net/pdo :) – tftd Apr 07 '15 at 15:33
  • @tftd First of all thank you for your time . I did what you said and now i am getting this `Fatal error: Cannot pass parameter 2 by reference in E:\xampp\htdocs\ptixiaki\livesearch.php on line 9` , Line 9 is this one `$stmt->bindParam(':q', '%'.$_GET['q'].'%');` – Waaaaat Apr 07 '15 at 15:44
  • You probably need to set two different parameters in the query instead of just one. So the first `:q` should become `:q1` and the second one `:q2`. Afterwards bind a value to those params by doing `$stmt->bindParam(':q1', '%'.$_GET['q'].'%')` and `$stmt->bindParam(':q2', '%'.$_GET['q'].'%')`. – tftd Apr 07 '15 at 17:12
  • @tftd If you mean somethink like that (http://prntscr.com/6qsrqc), it doesnt fix the error – Waaaaat Apr 07 '15 at 17:18
  • 1
    Yeah, something like that should be working. Try replacing `bindParam` with `bindValue` – tftd Apr 07 '15 at 18:53
  • @tftd I did what you said and now I am getting this error (http://prntscr.com/6qy810) – Waaaaat Apr 07 '15 at 23:01