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;
?>
First of all use the X in ajax! So don't send HTML hidden in plain text, but your data in XML
Split up the ajax requests, maybe use pages to show the data. So less data is transported.
Your biggest problem is that you don't use a database! XML files will be fast for like 100 entries but incredible slow compared to real databases like MySQL. After all you are reading in that file for every request.