Jump to content

[SOLVED] Sorry for double posting, PHP Live Search code question


Recommended Posts

 

Sorry for double posting. My first time here.

 

I found the PHP Ajax Live Search code here > http://www.w3schools.com/php/php_ajax_livesearch.asp

 

I edited an rss/xml feed for recent listings from a php directory I use the same as the links.xml file example they give & got it working > http://www.easysavannah.com/livesearch.html

 

Problem is I cannot get it to search the "description", only the "title" & "url". The "livesearch.php" listed here >> http://www.w3schools.com/php/php_ajax_livesearch.asp seems to mirror in ways the php for the MSN Live Search API code > http://dev.live.com/blogs/livesearch...01/08/445.aspx

 

w3school's code snippet:

 

Quote:

$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>"; 

 

MSN Live Search php snippett:

 

Quote:

echo('<li class="resultlistitem"><a href="' . $value->childNodes->item(2)->nodeValue . '">'); //Url echo('<h3>' . $value->childNodes->item(0)->nodeValue . '</h3></a>'); //Title echo('<p>' . $value->childNodes->item(1)->nodeValue . '</p>'); //Description } echo("</ul>"); 

 

I've tried a multitude of ways to add an echo into "w3"s code for allowing the code to search a description in an xml page.

 

Has anyone seen a solution for this? Any help would be appreciated. Thanks, Gene

 

PS. Code sample on index page of directory I'm testing > http://www.easysavannah.com/index2.html (middle below header)

 

I am able to take an rss/xml file from all listings in my directory(s) & convert it to a file like this one in 5-10 minutes > http://www.w3schools.com/php/links.xml  This is the example file in the php live search code at > http://www.w3schools.com/ . I've done maybe 6-7 different possible changes & none works. I am just looking for a way to allow the <descriptions> in the xml to be keyword searched & the listing title (& url) & description to show in live search dropdown. At present only the title & url are searched. I use friendly urls so it's the same search result always. I can edit out the <title></title> from the xml & switch the <description></description> tags to <title></title> w/ "replace" feature in Notepad but it's a pain. Any help would be really appreciated. Thanks, Gene

OKay i assume the XML looks like this

 

<pages>
<link>
<title>PHP Freaks - This is a sample</title>
<description>I am a description</description>
<url>http://www.phpfreaks.com/forums/index.php/topic,277452.0/topicseen.html</url>
</link>
<link>
<title>PHP Freaks Main Page</title>
<description>PHP Freaks is a website dedicated to learning and teaching PHP. Here you will find a forum consisting of 86915 members who have posted a total of 963580 </description>
<url>http://www.phpfreaks.com/forums</url>
</link>
</pages>

 

from that this should work fine

 

<?php
/**
$q='description'; // returns  1
$q='Freaks'; // returns  2

*/

$xmlDoc = new DOMDocument();
$xmlDoc->load('c:\277452.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++){
	$a=$x->item($i)->getElementsByTagName('title');
	$b=$x->item($i)->getElementsByTagName('description');
	$c=$x->item($i)->getElementsByTagName('url');
	if ($a->item(0)->nodeType==1){
		//find a link matching the search text
		if (stristr($a->item(0)->childNodes->item(0)->nodeValue,$q) ||
			stristr($b->item(0)->childNodes->item(0)->nodeValue,$q)){
			$hint .= "<a href='" .
			$c->item(0)->childNodes->item(0)->nodeValue .
			"' target='_blank'>" .
			$a->item(0)->childNodes->item(0)->nodeValue . "</a>".
			$b->item(0)->childNodes->item(0)->nodeValue . "<br />\n";
		}
	}
}
}

// 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;
?> 

:o It works great! Thank you so much. Do you mind if I post this solution at PhpMyDirectory boards? If you ever need any help over there let me know. Maybe I can help ya. I'll see how well we can tackle the css to get a better look maybe & post it back here. Thanks again, gene99  ;D
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.