Jump to content

Problem with Livesearch tutorial at w3schools


lilianev

Recommended Posts

Hi, this is my first time posting here. My apologies if I do it incorrectly.

 

The following is based off of the PHP Ajax Livesearch tutorial here: http://www.w3schools.com/php/php_ajax_livesearch.asp

 

My example is here: http://pr107m.psur.utk.edu/~lilia/campusmap/livesearch.html

 

Here is the PHP from my livesearch.php file:

<?php 
$xmlDoc=new DOMDocument("1.0");
$xmlDoc->load("genxml-buildings-depts-050411.xml");

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

//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('building');  
  $z=$x->item($i)->getElementsByTagName('department');
  if ($p = $y->item(0)->nodeType==1)
    { 
    //find a link matching the search text
    if (stristr($y->item($i)->childNodes->item(0)->nodeValue,$q) || stristr($z->item($i)->childNodes->item($i)->nodeValue,$q) )
      { 
      if ($hint=" ")
        { 
	$hint="<a href='#' target='_blank'>" . $z->item($i)->childNodes->item($i)->nodeValue . "</a> — ". $y->item($i)->childNodes->item(0)->nodeValue ."<br>";
	if (stristr($y->item($i)->childNodes->item(0)->nodeValue,$q) ) {
	$hint=$hint."<a href='#' target='_blank'>" .
	$y->item($i)->childNodes->item(0)->nodeValue . "</a>"; }
	}
   else
	{ 
	$hint=$hint . "<br /><a href='#' target='_blank'>" .
	$z->item(0)->childNodes->item(0)->nodeValue .
	"</a><br><a href='#' 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;
?> 

 

 

Here is the XML: http://pr107m.psur.utk.edu/~lilia/campusmap/genxml-buildings-depts-050411.xml

 

My Problem: I am trying to get a live search to work. Currently, it's only searching (or at least displaying search results) from the first <building> element. If you search A, M, C, etc. it will display results (Mathematics, Aconda Court). If you search any other letter, B, Y, G, etc., it returns "no suggestions." I've searched on forums most of the afternoon for a solution. It seems like a lot of folks have issues with the same W3schools tutorial, but not specifically this issue. I think I need to create another loop somewhere, but when I've looked at others' adaptations of the W3Schools tutorial, they're not using another loop. Which makes me think I've done something wrong.

 

Any help is appreciated. Thank you very much.

Link to comment
Share on other sites

Something tells me this isn't 100%, but you could tweak it from here (or post the exact problem you are getting, and we can help further).  BTW, one of the best descriptions ever posted by a newbie.

 

<?php 
$xmlDoc=new DOMDocument("1.0");
//"genxml-buildings-depts-050411.xml"
$xmlDoc->load('http://pr107m.psur.utk.edu/~lilia/campusmap/genxml-buildings-depts-050411.xml');

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

//get the q parameter from URL
$q= (isset($_GET['q'])) ? $_GET['q'] : NULL;

//lookup all links from the xml file if length of q>0
if (!empty($q))
{
   
$hint="";
for($i=0; $i<($x->length);$i++)
  {
  $y=$x->item($i)->getElementsByTagName('building');
  
  for($n = 0; $n < ($y->length); $n++) {
  $z=$x->item($i)->getElementsByTagName('department');
  
  
  
  for($num = 0; $num < ($z->length); $num++) {
	  if ($p = $y->item(0)->nodeType==1)
		{ 
		//find a link matching the search text
		 if (stristr($y->item($n)->nodeValue,$q) || stristr($z->item($num)->nodeValue,$q) )
		  { 
		  if ($hint=" ")
			{				
		  $hint = "<a href='#' target='_blank'>" . $z->item($num)->nodeValue . "</a> — ". $y->item($i)->nodeValue ."<br>";
		  if (stristr($y->item($n)->nodeValue,$q) ) {
		  $hint .= "<a href='#' target='_blank'>" .
		  $y->item($n)->nodeValue . "</a>"; }
		  }
		  else
		  { 
		  $hint .= "<br /><a href='#' target='_blank'>" .
		  $z->item(0)->nodeValue .
		  "</a><br><a href='#' target='_blank'>" .
		  $y->item(0)->nodeValue . "</a>";
			} 
		  }
		}
	  }
	}
}
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if (empty($hint))
  {
  $response="no suggestion";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;
?> 

Link to comment
Share on other sites

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.