?
Solved

live search of xml file part !!

Posted on 2009-12-23
8
Medium Priority
?
252 Views
Last Modified: 2012-05-08
Hi,
I have code provided by bportlock that should generate titles after searching for input variables in a text box.  For some reason the code is not generating results and I believe it's a syntax error but I don't see it. Hoping someone can help.

The error must be in RssIndex.php

The result of the current code can be seen here http://prontopage.net/livesearch/RssIndex.php 

(please remove the .txt portion of the filename)

thanks,
Chris
RssIndex.txt.php
RssFeed.txt.php
RssItem.txt.php
0
Comment
Question by:chrisj1963
  • 5
  • 2
8 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26116414
Wouldn't this be easier if you posted the code in the code snippet?  It makes the code visible for all of us and we don't have to guess about what might be lurking in a file download.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26116421
Here is RssIndex
<?php
        include_once 'RssFeed.php';
        include_once 'RssItem.php';
        
        $feed = new RssFeed();
        $feed->loadRssFileFrom( "http://prontopage.net/livesearch/googlecat4.xml" );
   
        // Demo of how to use the data
        //
        $pres3 = "";
        $arrayOfItems = $feed->getRssList();
        $newArray = $feed->matching("a search string");

        print_r( $newArray );
        
        // Remove trailing comma
        //
        $pres3 = substr( $pres3, 0, -1 );
     
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Reverse autocomplete</title>

<style type="text/css">
        .suggestion_list
{
        background: white;
        border: 1px solid;
        padding: 4px;
}
        
        .suggestion_list ul
{
        padding: 0;
        margin: 0;
        list-style-type: none;
}
        
        .suggestion_list a
{
        text-decoration: none;
        color: navy;
}
        
        .suggestion_list .selected
{
        background: navy;
        color: white;
}
        
        .suggestion_list .selected a
{
        color: white;
}

#autosuggest
{
        display: none;
}
    </style>
    <script language="Javascript" src="autocomplete1_files/autosuggest.js"></script>
    <script language="Javascript" src="autocomplete1_files/cursor.js"></script>
</head><body onload="setup()">
<div style="position: absolute; left: 98px; top: 2814px; display: none;" class="suggestion_list" id="autosuggest"><ul><li class="selected"><a href="javascript:false">William Harrison</a></li><li class=""><a href="javascript:false">Benjamin Harrison</a></li></ul></div>
<div style="width: 70%; float: left;">
  <h3><br></h3>

<table border="1" rules="none">
<tbody><tr>
<td width="320"><center><br>
</center>
<center>
Bidirectional autocomplete, also appending at both ends [<a href="#n2">note</a>]:<br>
<form onsubmit=";" name="test3" id="test3" action="autocomplete.html#l3">
<input value="William Harrniso" autocomplete="off" name="textbox2" id="textbox2" onclick="return(update(this))" onkeyup="return(update(this))" onkeydown="return(update(this))" onkeypress="return(update(this))" onmouseup="return(update(this))" onmousedown="return(update(this))" type="text">
                <script language="Javascript">
                        var pres3 = new Array(<?php echo $pres3; ?>);
                        new AutoSuggest3(document.getElementById('textbox2'),pres3);
                </script>
</form>
</center>
<label for="leftbox"><input type="hidden" id="leftbox" name="leftbox" maxlength="32" size="15" value="William&nbsp;H" disabled="disabled"></label>&nbsp;<label for="rightbox"><input type="hidden" id="rightbox" name="rightbox" maxlength="32" size="15" value="arrniso" disabled="disabled"></label>
</td>
<td width="10%">
</td>
<td>

<i>Enter names of American presidents (autocomplete is done on the whole name)... <br>
Try for example "Theodore Roosevelt", then change "Theodore" to "Franklin".</i>
</td>
</tr>
</tbody></table>
</div>

</body></html>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26116426
Here is RssFeed
<?php



class RssFeed  {

	// Required items
	//
     protected $rsTitle;                // The Title to display
     protected $rsLink;                 // The link to the item
     protected $rsDescription;          // The text description
     protected $rsLanguage;             // The language it is written in
     
     // Optional items
     //
     protected $rsImageLink;            // The link to an image
     protected $rsImageHeight;          // The height of the image
     protected $rsImageWidth;           // The width of the image
     protected $rsImageTitle;           // The title of the image. 001 According to W3C this should be
      							// the same as "rsTitle" so this field is redundant
      							

	// The RSS items (an array of objects)
	//
	protected $rssList;				// A list of RSS feed items
	
	

     // ----- Constructor -----------------------------------------------------

     function RssFeed() {



          $this->initialise(  );

     }


     // ----- Setter and getter methods ---------------------------------------

     // Set the Title to display
     //
     function setRsTitle( $prmRsTitle ) {
          $this->rsTitle = $this->escapeText( $prmRsTitle );

     	// 001 Set the Image title to be the same as this title
     	//
		$this->rsImageTitle = $this->rsTitle;
     }

     // Get the Title to display
     //
     function getRsTitle() {
          return $this->rsTitle;
     }

     // Set the link to the item
     //
     function setRsLink( $prmRsLink ) {
     	
     	if ( strpos( $prmRsLink, "http://") !== 0 )
          	$this->rsLink = "http://$prmRsLink";
     	else
          	$this->rsLink = $prmRsLink;
     }

     // Get the link to the item
     //
     function getRsLink() {
          return $this->rsLink;
     }

     // Set the text description
     //
     function setRsDescription( $prmRsDescription ) {
          $this->rsDescription = $this->escapeText( $prmRsDescription );
     }

     // Get the text description
     //
     function getRsDescription() {
          return $this->rsDescription;
     }

     // Set the language it is written in
     //
     function setRsLanguage( $prmRsLanguage ) {
          $this->rsLanguage = $this->escapeText( $prmRsLanguage );
     }

     // Get the language it is written in
     //
     function getRsLanguage() {
          return $this->rsLanguage;
     }

     // Set the link to an image
     //
     function setRsImageLink( $prmRsImageLink ) {

          $this->rsImageLink = $prmRsImageLink;
     }

     // Get the link to an image
     //
     function getRsImageLink() {
          return $this->rsImageLink;
     }

     // Set the height of the image
     //
     function setRsImageHeight( $prmRsImageHeight ) {
          $this->rsImageHeight = intval($prmRsImageHeight);
     }

     // Get the height of the image
     //
     function getRsImageHeight() {
          return $this->rsImageHeight;
     }

     // Set the width of the image
     //
     function setRsImageWidth( $prmRsImageWidth ) {
          $this->rsImageWidth = intval($prmRsImageWidth);
     }

     // Get the width of the image
     //
     function getRsImageWidth() {
          return $this->rsImageWidth;
     }

     // Set the title of the image
     //
     function setRsImageTitle( $prmRsImageTitle ) {
          $this->rsImageTitle = $this->escapeText( $prmRsImageTitle );
     }

     // Get the title of the image
     //
     function getRsImageTitle() {
          return $this->rsImageTitle;
     }

     // Set a location that the image links to
     //
     function setRsImageUrl( $prmRsImageUrl ) {
     	if ( strpos( $prmRsImageUrl, "http://") !== 0 )
          	$this->rsImageUrl = "http://$prmRsImageUrl";
     	else
          	$this->rsImageUrl = $prmRsImageUrl;
     }

     // Get a location that the image links to
     //
     function getRsImageUrl() {
          return $this->rsImageUrl;
     }


	// Return a list of RSS objects
	//
	function getRssList() {
		return $this->rssList;
	}	


	// Add an array of RSS objects
	//
	function setRssList( $arr ) {
		if ( !is_array($arr) )
			return;
			
		foreach( $arr as $anObj )
			if ( !is_object( $anObj) )
				return;
				
		$this->rssList = $arr;
	}	




     // ----- Private methods -------------------------------------------------

	// 002 Return a count of the number of items in the feed
	//
	function itemCount() {
		return count( $this->rssList );
	}
	
	
	// 002 Return an item from the list as selected by an index
	//
	// Returns: An Rss Item Object
	//
	function fetchItem( $number ) {
		$obj = new RssItem( "", "", "" );
		
		if ( $number >= 0 && $number < count( $this->rssList ) )
			$obj = $this->rssList [ $number ];
			
		return $obj;
	}
	
	
	// Escape text for XML
	//
	private function escapeText( $text ) {
		return str_replace('&#039;', '&apos;', htmlspecialchars($text, ENT_QUOTES));
	}
	
	
	// Extract top level XML data using an XML path generated object
	//
	private function extractXmlData( $pathObject ) {
		return (string) $pathObject[0];
	}



	// Parse a string containing XML and populate the class properties including the 
	// creation of an XML list (an array of XML objects)
	//
     private function parse( $xml ) {

		// XML Loaded, now dig out the relevant bits
		//
		$this->initialise();

		$this->rsTitle       = $this->extractXmlData( $xml->xpath("/rss/channel/title")  ) ;
		$this->rsDescription = $this->extractXmlData( $xml->xpath("/rss/channel/description")  ) ;
		$this->rsLink        = $this->extractXmlData( $xml->xpath("/rss/channel/link")  ) ;
		
		// Do the items. Due to a PHP/XML bug, it is best to "for" loop these
		//
		$itemCount = count($xml->xpath("/rss/channel/item")) ;
		$items = $xml->xpath("/rss/channel/item");
		foreach( $items as $anItem ) {
			$this->rssList [] = new RssItem( 
												(string) $anItem->link, 
												(string) $anItem->description, 
												(string) $anItem->title 
											);		
		}
     }
     
     



     // Initialise fields
     //
     protected function initialise() {
          $this->rsTitle              = "";
          $this->rsLink               = "";
          $this->rsDescription        = "";
          $this->rsLanguage           = "";
          $this->rsImageLink          = "";
          $this->rsImageHeight        = 0;
          $this->rsImageWidth         = 0;
          $this->rsImageTitle         = "";
          $this->rsImageUrl           = "";
          $this->rsItemLink           = "";
          $this->rsItemDescription    = "";
          $this->rsItemTitle          = "";
          
          $this->rssList = array();

     }
     




     // ----- Public methods --------------------------------------------------



	// Add a new rss object
	//
	function add( $rssObj) {
		$this->rssList [] = $rssObj;
	}
	
	

     
     
     // Load an XML RSS feed from a file on the server
     //
	// Parameters: $fullPathName - The folder and filename to use to locate the XML file
	//
	// Returns: "" on success or an error message on failure
	//	
     function loadRssFileFrom( $fullPathName ) {
     	$error     = "";
          $pathParts = pathinfo( $fullPathName );
          $fileName  = $pathParts['filename'];

		// 003 Beware of empty URLs
		//
		if ( $fullPathName != "" ) {                    

	     	$xml = simplexml_load_file( $fullPathName );
	     	
	
	     	if ( $xml === false )
	     		$error = "Error - could not open RSS file $fileName"; 
	     	else
	     		$this->parse( $xml );
		}
		
     	return $error;
     }
     
     

     // Alter the list of items so that only those matching the 
     // search string are reatined. returns an array of titles
     // of elements that matched the search
     //
     function matching( $searchTerm ) {

          $newList = array();
          foreach( $this->rssList as $anIndex => $anItem ) 
               if ( $anItem->hasText( $searchTerm ) )
                    $newList [$anIndex] = $anItem->getRsItemTitle();

          return $newList;
     }


     


} // End of class RssFeed

?>

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26116430
Here is RssItem
<?php



class RssItem {

     protected $rsItemLink;             // A link to an individual item
     protected $rsItemDescription;      // The individual items description
     protected $rsItemTitle;            // The individual item title



     // ----- Constructor -----------------------------------------------------

     function __construct( $link, $desc, $title ) {
          $this->setRsItemLink( $link );
          $this->setRsItemDescription( $desc );
          $this->setRsItemTitle( $title );
     }


     // ----- Setter and getter methods ---------------------------------------


     // Set a link to an individual item
     //
     function setRsItemLink( $prmRsItemLink ) {
     	if ( strpos( $prmRsItemLink, "http://") !== 0 )
          	$this->rsItemLink = "http://$prmRsItemLink";
     	else
          	$this->rsItemLink = $prmRsItemLink;
     }

     // Get a link to an individual item
     //
     function getRsItemLink() {
          return $this->rsItemLink;
     }

     // Set the individual items description
     //
     function setRsItemDescription( $prmRsItemDescription ) {
          $this->rsItemDescription = $prmRsItemDescription;
     }

     // Get the individual items description
     //
     function getRsItemDescription() {
          return $this->rsItemDescription;
     }

     // Set the individual item title
     //
     function setRsItemTitle( $prmRsItemTitle ) {
          $this->rsItemTitle = $prmRsItemTitle;
     }

     // Get the individual item title
     //
     function getRsItemTitle() {
          return $this->rsItemTitle;
     }


     // Check if either the title or the description includes a certain 
     // text string
     //
     function hasText( $searchTerm ) {

          return ( 
                   stripos( $this->rsItemTitle, $searchTerm ) !== false || 
                   stripos( $this->rsItemDescription, $searchTerm ) !== false 
                 );
     }


} // End of class RssItem

?>

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 26116454
That brings us to a total of 500+ lines of code, and we still do not have the javaScript.  Any chance you could try to distill this down to a question instead of asking us to engage in a debugging exercise?  Maybe if you tell us what the inputs are supposed to be and what the expected outputs are, we would have something more to go on.  I'd like to help if you can tell us what has got you stumped, but ultimately it is you who gets both the pleasure and reward of making your code productive.
0
 

Author Comment

by:chrisj1963
ID: 26116719
Thanks for adding the code as you did.  OK.  This code works (but only searches titles) when what I want is to search titles AND descriptions and generate Title results only..

In RssIndex.php
9        // Demo of how to use the data
10       //
11        $pres3 = "";
12        $arrayOfItems = $feed->getRssList();
13        foreach( $arrayOfItems as $anItem ) {
 14             $pres3 .= '"' . htmlspecialchars( $anItem->getRsItemTitle(), ENT_QUOTES ) . '",';
 15       }
 16      
  17      // Remove trailing comma
   18     //
   19     $pres3 = substr( $pres3, 0, -1 );

Now just so all can see what that result looks like, I created an http://www.prontopage.net/livesearch/RssIndex2.php (again note that these results are only showing Titles when the search term is found in the Title itself. It does not search the Description too, as I would like. Just start entering letters and you will see results.

The updated RssIndex.php (per expert bportlock) which is included in my original post (and what Ray Paseur posted for me above) is supposed to (along with RssFeed.php and RssItem.php) produce a list of Titles.  This list of Titles would be generated if the key term entered in the text box is found in the Title OR its associated description in the http://prontopage.net/livesearch/googlecat4.xml file.

RssIndex.php updated (but does not generate any results)
8        // Demo of how to use the data
9        //
10        $pres3 = "";
 11       $arrayOfItems = $feed->getRssList();
  12      $newArray = $feed->matching("a search string");
13
 14       print_r( $newArray );

Is the problem with syntax? If not is there any obvious error that might be preventing the results that I am seeking?

Thanks.
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 1000 total points
ID: 26117695
$newArray = $feed->matching("a search string");

Well, it will not produce any results unless you replace "a search string" with the string you want to find in the titles and descriptions. For instance, if you wanted to list only the titles of those feed items containing "life" then

$newArray = $feed->matching("life");
print_r($newArray);
0
 

Author Closing Comment

by:chrisj1963
ID: 31669619
got it. Now I just need to figure out how to make that work with the RssIndex.php search box.
thanks very much.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question