optikalefx Posted August 15, 2007 Share Posted August 15, 2007 ive got this code so far for a live search, which does work <?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>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 were found // or to the correct values if ($hint == "") { $response="no suggestion"; } else { $response=$hint; }//output the response echo $response; ?> on the tutorial site it says for purpose of demonstration they limit their search results to 8. on my exmample i get much more than 8. How can i add a parameter here so it only grabs 8? Link to comment https://forums.phpfreaks.com/topic/64997-limit-search-results/ Share on other sites More sharing options...
keeB Posted August 15, 2007 Share Posted August 15, 2007 Change for($i=0; $i<($x->length); $i++) To for($i=0; $i<8; $i++) Link to comment https://forums.phpfreaks.com/topic/64997-limit-search-results/#findComment-324380 Share on other sites More sharing options...
optikalefx Posted August 15, 2007 Author Share Posted August 15, 2007 i tried that, that just limits how many letters of the word it searches. It gave me 10 results when I changed that parameter. Whats happening is that it grabs every <title> from the xml document, and then searches within those characters. So I gotta find a way to limit how many <title>s it grabs Link to comment https://forums.phpfreaks.com/topic/64997-limit-search-results/#findComment-324670 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.