title: Ajax live search of xml document
body: <p>I'm building a employee directory with over 4k employees. The method im using works great on a few hundred but takes way to long to search the over 4k employees. I'm looking for a way to speed this up or a better way to do the search altogether.</p>
  
  <p>This is the ajax running on the main php page.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function showResult(str)
  {
  if (str.length==0)
    { 
    document.getElementById("livesearch").innerHTML="";
    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 &amp;&amp; xmlhttp.status==200)
      {
      document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
      }
    }
  xmlhttp.open("GET","empSearch.php?q="+str,true);
  xmlhttp.send();
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>This is the php that searches the xml document and formats the results.</p>
  
  <pre><code>&lt;?php
  $xmlDoc=new DOMDocument();
  $xmlDoc-&gt;load('find_search_inc.xml');
  
  $x=$xmlDoc-&gt;getElementsByTagName('emp');
  
  $q=$_GET["q"];
  
  if (strlen($q)&gt;=1)
  {
  $hint="";
  
  for($i=0; $i&lt;($x-&gt;length); $i++)
    {
    $y=$x-&gt;item($i)-&gt;getElementsByTagName('fname');
    $z=$x-&gt;item($i)-&gt;getElementsByTagName('lname');
    $w=$x-&gt;item($i)-&gt;getElementsByTagName('location');
    $p=$x-&gt;item($i)-&gt;getElementsByTagName('position');
    $id=$x-&gt;item($i)-&gt;getElementsByTagName('id');
  
    if ($y-&gt;item(0)-&gt;nodeType==1)
      {
      if (stristr($y-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue,$q)||stristr($z-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue,$q))
        {
        if ($hint=="")
          {   
          $hint="&lt;div class='emp'&gt; &lt;dl class='empInfo'&gt;
          &lt;dd class='info'&gt;&lt;strong&gt;".$y-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue." ".$z-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue."&lt;/strong&gt;&lt;/dd&gt;
          &lt;dd class='info'&gt;&lt;img src='images/emailIcon.png' /&gt;email@leprinofoods.com&lt;/dd&gt;
          &lt;dd class='info'&gt;&lt;img src='images/phoneIcon.png' /&gt;". $p-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . "&lt;/dd&gt;
          &lt;dd class='info'&gt;&lt;img src='images/briefcaseIcon.png' /&gt;". $id-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . "&lt;/dd&gt;
          &lt;dd class='info'&gt;&lt;img src='images/locationIcon.png' /&gt;". $id-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . "&lt;/dd&gt;
          &lt;dd class='empImage'&gt;&lt;img src='images/default.png' /&gt;&lt;/dd&gt;
  
          &lt;/dl&gt;&lt;/div&gt;";
          }
        else
          {
           $hint=$hint ."&lt;div class='emp'&gt; &lt;dl class='empInfo'&gt;
          &lt;dd class='info'&gt;&lt;strong&gt;".$y-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue." ".$z-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue."&lt;/strong&gt;&lt;/dd&gt;
          &lt;dd class='info'&gt;&lt;img src='images/emailIcon.png' /&gt; email@leprinofoods.com&lt;/dd&gt;
          &lt;dd class='info'&gt;&lt;img src='images/phoneIcon.png' /&gt; ". $p-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . "&lt;/dd&gt;
          &lt;dd class='info'&gt;&lt;img src='images/briefcaseIcon.png' /&gt;". $id-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . "&lt;/dd&gt;
          &lt;dd class='info'&gt;&lt;img src='images/locationIcon.png' /&gt;". $id-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . "&lt;/dd&gt;
          &lt;dd class='empImage'&gt;&lt;img src='images/default.png' /&gt;&lt;/dd&gt;
          &lt;/dl&gt;&lt;/div&gt;";
          }
        }
      }
    }
  }
  if ($hint=="")
    {
    $response="Sorry that employee was not found.";
    }
  else
    {
    $response=$hint;
    }
  echo $response;
  ?&gt;
  </code></pre>