Jump to content

AJAX and PHP LiveSearch


slimboyfatz32

Recommended Posts

Hi , I have recently touched on the AJAX/PHP LiveSearch tutorial on the W3Schools website , the problem i am up against is that my searches return a response to a letter (ie : D) that appears anywhere in the chosen search entry , not just the beginning (which is how i would like it , and more like googles auto-complete function) see for yourself http://www.atrium-improvements.co.uk/search.html the code for the PHP is here ....<?php$xmlDoc = new DOMDocument();$xmlDoc->load("links.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>0if (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='_self'>" . $y->item(0)->childNodes->item(0)->nodeValue . "</a>"; } else { $hint=$hint . "<br /><a href='" . $z->item(0)->childNodes->item(0)->nodeValue . "' target='_self'>" . $y->item(0)->childNodes->item(0)->nodeValue . "</a>"; } } } }}// Set output to "Business Type Not Found" if no hint were found// or to the correct valuesif ($hint == "") { $response="Business Type Not Found"; }else { $response=$hint; } //output the responseecho $response;?>

Link to comment
Share on other sites

Instead of using stristr to do the check, use stripos and check if the return value is 0, that will mean that the substring starts the search string. http://www.php.net/manual/en/function.stripos.php
thanks very much for the reply , being a noob to PHP , how would i change the code posted above to work in such a manner as you have mentioned ??cheers again
Link to comment
Share on other sites

Find the if statement that has stristr in it. From the reference page for stristr apparently the first parameter is the haystack to search in, and the second parameter is the needle to search for:http://www.php.net/manual/en/function.stristr.phpFrom the stripos reference page it looks like it has the parameters in the same order, so you can just replace the function name with stripos and check if the return value is 0, there are examples on the stripos page to do that. You need to use the === operator instead of just == to make sure it is the number 0 and not boolean false.if (stripos($haystack, $needle) === 0)//haystack starts with needle

Link to comment
Share on other sites

Find the if statement that has stristr in it. From the reference page for stristr apparently the first parameter is the haystack to search in, and the second parameter is the needle to search for:http://www.php.net/manual/en/function.stristr.phpFrom the stripos reference page it looks like it has the parameters in the same order, so you can just replace the function name with stripos and check if the return value is 0, there are examples on the stripos page to do that. You need to use the === operator instead of just == to make sure it is the number 0 and not boolean false.if (stripos($haystack, $needle) === 0)//haystack starts with needle
Thanks justsomeguy , how exactly would i add this to the script i originally posted , many thanks again!!
Link to comment
Share on other sites

Good god. The links I gave you have all the information you need, but here's the version where you don't have to understand anything. Replace this line:if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))with this:if (stripos($y->item(0)->childNodes->item(0)->nodeValue,$q) === 0)

Link to comment
Share on other sites

Good god. The links I gave you have all the information you need, but here's the version where you don't have to understand anything. Replace this line:if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))with this:if (stripos($y->item(0)->childNodes->item(0)->nodeValue,$q) === 0)
Ok thanks for the help , and chill out baby !!!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...