Hi guys,

I've wrote some Ajax (heavily inspired by the Livesearch on W3Tutorials may I add :P) that searches an xml document and outputs information accordingly.

It works kinda fine, but since the xml document is 200kb long, and it's looping through it like something crazy, it's taking a few minutes for every search and I've just managed to temporarily crash the web server :P

I'm after something that'll hopefully optimise the code, but I can't think for the life of me what the best way around this is.

Here's my Javascript code at present:

Code JavaScript:
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("get_xml.xml");
 
$x=$xmlDoc->getElementsByTagName('user');
 
//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('name');
 $z=$x->item($i)->getElementsByTagName('collar');
 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='#' onclick='employee_form.Line_Manager_Staff_No.value=" . 
    $z->item(0)->childNodes->item(0)->nodeValue . 
    ";checknumber('Line_Manager_Staff_No','displayusername');'>" . 
    $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
    }
   else
    {
    $hint=$hint . "<br /><a href='#' onclick='employee_form.Line_Manager_Staff_No.value=" . 
    $z->item(0)->childNodes->item(0)->nodeValue . 
    ";checknumber('Line_Manager_Staff_No','displayusername');'>" . 
    $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;
?>
Any help would be awesome, thank you.