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.
This is the ajax running on the main php page.
<script type="text/javascript">
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 && xmlhttp.status==200)
{
document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","empSearch.php?q="+str,true);
xmlhttp.send();
}
</script>
This is the php that searches the xml document and formats the results.
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load('find_search_inc.xml');
$x=$xmlDoc->getElementsByTagName('emp');
$q=$_GET["q"];
if (strlen($q)>=1)
{
$hint="";
for($i=0; $i<($x->length); $i++)
{
$y=$x->item($i)->getElementsByTagName('fname');
$z=$x->item($i)->getElementsByTagName('lname');
$w=$x->item($i)->getElementsByTagName('location');
$p=$x->item($i)->getElementsByTagName('position');
$id=$x->item($i)->getElementsByTagName('id');
if ($y->item(0)->nodeType==1)
{
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)||stristr($z->item(0)->childNodes->item(0)->nodeValue,$q))
{
if ($hint=="")
{
$hint="<div class='emp'> <dl class='empInfo'>
<dd class='info'><strong>".$y->item(0)->childNodes->item(0)->nodeValue." ".$z->item(0)->childNodes->item(0)->nodeValue."</strong></dd>
<dd class='info'><img src='images/emailIcon.png' />email@leprinofoods.com</dd>
<dd class='info'><img src='images/phoneIcon.png' />". $p->item(0)->childNodes->item(0)->nodeValue . "</dd>
<dd class='info'><img src='images/briefcaseIcon.png' />". $id->item(0)->childNodes->item(0)->nodeValue . "</dd>
<dd class='info'><img src='images/locationIcon.png' />". $id->item(0)->childNodes->item(0)->nodeValue . "</dd>
<dd class='empImage'><img src='images/default.png' /></dd>
</dl></div>";
}
else
{
$hint=$hint ."<div class='emp'> <dl class='empInfo'>
<dd class='info'><strong>".$y->item(0)->childNodes->item(0)->nodeValue." ".$z->item(0)->childNodes->item(0)->nodeValue."</strong></dd>
<dd class='info'><img src='images/emailIcon.png' /> email@leprinofoods.com</dd>
<dd class='info'><img src='images/phoneIcon.png' /> ". $p->item(0)->childNodes->item(0)->nodeValue . "</dd>
<dd class='info'><img src='images/briefcaseIcon.png' />". $id->item(0)->childNodes->item(0)->nodeValue . "</dd>
<dd class='info'><img src='images/locationIcon.png' />". $id->item(0)->childNodes->item(0)->nodeValue . "</dd>
<dd class='empImage'><img src='images/default.png' /></dd>
</dl></div>";
}
}
}
}
}
if ($hint=="")
{
$response="Sorry that employee was not found.";
}
else
{
$response=$hint;
}
echo $response;
?>