0

Good Day,

I have a page where the list of data taken from the db are being fetched in an array. Here's the code:

<table width="100%">
<thead>
<tr>

<th>Name:</th>
<th>Address:</th>
<th>Birthday:</th>

</tr>
</thead>
$samp=mysql_query("select * from client order by id asc");
$counter=0;
while($row=mysql_fetch_array($samp))
    {
    $id         =   $row['id'];
    $name       =   $row['name'];
    $address    =   $row['address'];
    $birthday   =   $row['birthday'];


        if($counter%2)
        {
        ?>
        <tbody>
        <tr id="<?php echo $id; ?>">
        <?php } else { ?>
        <tr id="<?php echo $id; ?>">
        <?php } ?>
        <td>
        <span class="text"><?php echo $id.".  ".$name; ?></span>
        </td>
        <td>
        <span class="text"><?php echo $address; ?></span> 
        </td>
        <td>
        <span class="text"><?php echo $birthday; ?></span> 
        </td>
        <tr>
       <!--and so on -->
        </tbody>

I saw this reference Live Search Jquery and have tried if this will gonna work with my program. But much to my surprise it only appears to work for the pre-defined values. But when I tried to use it to my table which has the this Live Search didn't work.

Too bad, since this reference is what am really trying to achieve. Since it already searches for the words without waiting for the user to complete his/her search. Anyone who has an opinion?

Community
  • 1
  • 1
MoonPrincess
  • 75
  • 1
  • 1
  • 10
  • i'm having a hard time understanding your question. What exactly are you trying to achieve? Where does the code not do what you want it to do? What did you expect to happen? what actually happens? – Caweren Jul 17 '14 at 07:35
  • @Caweren what am trying to do is to create a searchbox where a user once typed his/her search (even just a letter) would trigger the search function and call the fields that the user has been looking for from the db. Just like the link I have posted above. But when I tried it on, it didn't work. So I thought perhaps because the values I've placed on the columns taken from the db. Since the code seems to work only for pre-defined values. – MoonPrincess Jul 19 '14 at 02:48

1 Answers1

0

Test it:

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
function showResult(str)
{
if (str.length==0)
  {
  document.getElementById("livesearch").innerHTML="";
  return;
  }

  xmlhttp=new XMLHttpRequest();

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
<title>main page</title></head>
<body>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>

</body>
</html>

livesearch.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?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 were found
// or to the correct values
if ($hint=="")
  {
        $response="no suggestion";
  }
else
  {
        $response=$hint;
  }

//output the response
echo $response;
?> 

</body>
</html>

links.xml

<?xml version="1.0" encoding="utf-8"?>
<links>
    <link>
        <title>about</title>
        <url>main.html</url>
    </link>

    <link>
        <title>Gmail</title>
        <url>www.google.com</url>
    </link>

    <link>
        <title>yahoo</title>
        <url>www.google.com</url>
    </link>

    <link>
        <title>bing</title>
        <url>www.google.com</url>
    </link>

    <link>
        <title>torrent search</title>
        <url>www.google.com</url>
    </link>

    <link>
        <title>Zend</title>
        <url>www.google.com</url>
    </link>
</links>
Teerath Kumar
  • 488
  • 5
  • 15