查询自动填充中的所有类别

时间:2018-04-06 02:51:50

标签: javascript php ajax database wordpress

首先我要说的是,我要谈论的网站是一个Wordpress网站。其目的是充当各种业务类型(doctors, lawyers, realtors, etc)的目录。

现在有了这个问题,这就是问题所在:

一个月前,我的一位同事和我在他的帐户上发布了一个问题,询问StackOverflow是否有人可以帮助我们在我们的主题现有search bar之一上实施实时自动提供功能。以下是此链接:Implementing Autocomplete on a Wordpress site

帮助我们的人非常慷慨,并且基本上构建了大量的Javascript以创建该功能并且它非常出色,但是......有一个问题。

问题是我们只能根据信件进行查询。我的意思是,我们不能查询每一个医生类别,除非它有例如字母“a”或“abc”。

我们想要的是,当您在第一个search box中选择“医生”时,所有医生类别都将显示在第3个search box中,并且甚至不必担心基于一封信或任何事情。该程序应该知道,一旦我们在第一个"Doctor"中选择search box,就应该显示Doctor的所有子类别。

查询有4个组件。它从我们找到第三个“搜索框”的网站上的自定义Javsacript代码开始,然后调用autosearch函数进行搜索。这是:

document.getElementById("search_keywords").onkeyup= function() {
  var firstCat = document.getElementById("search_categories");
  var valCat = firstCat.options[firstCat.selectedIndex].value;
  listingsLiveSearch(this.value, valCat);
}
document.getElementById("search_keywords").onclick= function() {
  var firstCat = document.getElementById("search_categories");
  var valCat = firstCat.options[firstCat.selectedIndex].value;
  listingsLiveSearch("a", valCat);
}

第二部分是自定义Javascript代码,位于此处,您可以在其中找到'listingsLiveSearch()'函数:https://pastebin.com/NUFAqtFy

第三部分是调用XML文件的php文件,其中每个类别(医生,律师等)都有自己的php文件和XML文件。这是当你点击医生时调用的php文件:

<?php
//This PHP file is reponsible for accessing its associated xml document and         
creating divs for each item if they match the user's entered text.
$xmlDoc = new DOMDocument();
$xmlDoc->load("links_Doctor.xml");

$x=$xmlDoc->getElementsByTagName('link');

//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('title');
     $z=$x->item($i)->getElementsByTagName('url');
     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='" .
         $z->item(0)->childNodes->item(0)->nodeValue .
         "' target='_blank'>" .
         $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
         } else {
         $hint=$hint . "<br /><a href='" .
         $z->item(0)->childNodes->item(0)->nodeValue .
         "' target='_blank'>" .
         $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint was found
// or to the correct values
if (($hint=="")) {
  $response="No Suggestions Found";
} else {
  $response=$hint;
}

//output the response
echo $response;
?>

以下是医生类别的XML示例,其中包含所有子类别的列表:

<pages>

<link>
<title>Acupuncture</title>
<url>https://www.visitblue.com/listing-category/acupuncture</url>
</link>

<link>
<title>Bariatric Surgery</title>
<url>https://www.visitblue.com/listing-category/bariatric-surgery</url>
</link>

<link>
<title>Cardiology</title>
<url>https://www.visitblue.com/listing-category/cardiology</url>
</link>

<link>
<title>Chiropractic Physical Therapy</title>
<url>https://www.visitblue.com/listing-category/chiropractic-physical-therapy</url>
</link>

<link>
<title>Cosmetic Specialist</title>
<url>https://www.visitblue.com/listing-category/cosmetic-specialist</url>
</link>

<link>
<title>Dentistry</title>
<url>https://www.visitblue.com/listing-category/dentistry</url>
</link>

<link>
<title>Family Medicine</title>
<url>https://www.visitblue.com/listing-category/family-medicine</url>
</link>

<link>
<title>Fertility</title>
<url>visitblue.com/listing-category/fertility</url>
</link>

<link>
<title>General Surgery</title>
<url>https://www.visitblue.com/listing-category/general-surgery</url>
</link>

<link>
<title>Gynecology</title>
<url>https://www.visitblue.com/listing-category/gynecology</url>
</link>

<link>
<title>Internal Medicine</title>
<url>https://www.visitblue.com/listing-category/internal-medicine</url>
</link>

<link>
<title>Lasik</title>
<url>https://www.visitblue.com/listing-category/lasik</url>
</link>

<link>
<title>Medical Imaging</title>
<url>https://www.visitblue.com/listing-category/medical-imaging</url>
</link>

<link>
<title>Neurology</title>
<url>https://www.visitblue.com/listing-category/neurology</url>
</link>

<link>
<title>Neurosurgery</title>
<url>https://www.visitblue.com/listing-category/neurosurgery</url>
</link>

<link>
<title>Optometry</title>
<url>https://www.visitblue.com/listing-category/optometry</url>
</link>

<link>
<title>Orthopaedic Surgery</title>
<url>https://www.visitblue.com/listing-category/orthopedic-surgery</url>
</link>

<link>
<title>Orthopaedics</title>
<url>https://www.visitblue.com/listing-category/orthopedics</url>
</link>

<link>
<title>Pain Management</title>
<url>https://www.visitblue.com/listing-category/pain-management</url>
</link>

<link>
<title>Pediatric Dentistry</title>
<url>https://www.visitblue.com/listing-category/pediatric-dentistry</url>
</link>

<link>
<title>Pediatrics</title>
<url>https://www.visitblue.com/listing-category/pediatrics</url>
</link>

<link>
<title>Pharmacy</title>
<url>https://www.visitblue.com/listing-category/pharmacy</url>
</link>

<link>
<title>Plastic Surgery</title>
<url>https://www.visitblue.com/listing-category/plastic-surgery</url>
</link>

<link>
<title>Podiatry</title>
<url>https://www.visitblue.com/listing-category/podiatry</url>
</link>

<link>
<title>Psychotherapy</title>
<url>https://www.visitblue.com/listing-category/psychotherapy</url>
</link>

<link>
<title>Sleep Apnea</title>
<url>https://www.visitblue.com/listing-category/sleep-apnea</url>
</link>

<link>
<title>Spine Care</title>
<url>https://www.visitblue.com/listing-category/spine-care</url>
</link>

<link>
<title>Spine Surgery</title>
<url>https://www.visitblue.com/listing-category/spine-surgery</url>
</link>

<link>
<title>Urgent Care</title>
<url>https://www.visitblue.com/listing-category/urgent-care</url>
</link>

<link>
<title>Urology</title>
<url>https://www.visitblue.com/listing-category/urology</url>
</link>

</pages>

如果你想知道为什么XML文件中有一个链接,最初我们有了它,当你点击搜索建议时,它会自动带你到页面,但后来我们改了它,所以链接是任意的。

0 个答案:

没有答案