dtcmadj31951 2013-11-21 23:05
浏览 42

Ajax实时搜索xml文档

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;
?>
  • 写回答

1条回答 默认 最新

  • dongzhanlu8890 2013-11-21 23:33
    关注

    Use XML

    First of all use the X in ajax! So don't send HTML hidden in plain text, but your data in XML

    Split up AJAX Requests

    Split up the ajax requests, maybe use pages to show the data. So less data is transported.

    Use a database

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大