Google Answers Logo
View Question
 
Q: Need code to display message when no results are found in XML feed. ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Need code to display message when no results are found in XML feed.
Category: Computers > Programming
Asked by: nicnack23-ga
List Price: $20.00
Posted: 11 Aug 2006 03:08 PDT
Expires: 10 Sep 2006 03:08 PDT
Question ID: 754955
I am an affiliate of Yahoo Search Marketing, I am having a problem
with some of my code.  I am not a programmer so therfore am not sure
how to fix the problem.

I need a message to appear on the page which says "sorry no results
found" when a searcher searches for a term where Yahoo does not have
an advertiser.  Therefore if no results found found then display
message on screen.

The page is writen in PHP and the feed is in XML.

Any more info just let me know...

Request for Question Clarification by sycophant-ga on 11 Aug 2006 05:04 PDT
Hi, 

I should be able to provide this code for you... Can you provide
access to the source code of that page? If necessary remove any
passwords or access codes that you do not want revealed.

Regards,
Sycophant

Clarification of Question by nicnack23-ga on 11 Aug 2006 06:55 PDT
Yes of course, where would you like me to send the attachment to.

Thanks

Request for Question Clarification by sycophant-ga on 11 Aug 2006 14:10 PDT
Hi, 

I am not able to provide an email address.

Can you upload the file to a web or FTP site that I can download it
from? If you do not have one, a service like Upload2 (
http://upload2.net/ ) will allow you to put a file online for
download.

Regards,
Sycophant

Clarification of Question by nicnack23-ga on 12 Aug 2006 00:44 PDT
Hi it is probably quicker to just cut and paste the code to you, see
below, any questions let me know, the first set of code gathers the
data and the second set displays it:

<?php
  // This script transforms XML -> HTML using XML libraries
  // Accepts HTML form input (GET or POST) for search keywords

  // Enable Globals may be turned off
  $QUERY_STRING = $_SERVER['QUERY_STRING'] ;
  $REQUEST_METHOD = $_SERVER['REQUEST_METHOD'] ;

 // Specify URL to XML and XSL documents
  $xmlDoc = 'http://xml.uk.overture.com/d/search/p/standard/eu/xml/rlb/';

// Load "Keywords" query from form (POST or GET) and URL encode
  if ($REQUEST_METHOD=='POST') {
    $data = $_POST['Keywords'];
    $data = ereg_replace(" ", "+", $data);
    $data = ereg_replace("%20", "+", $data);
  } elseif ($QUERY_STRING) {
    $data = $_GET['Keywords'] ;
    $data = ereg_replace(" ", "+", $data);
    $data = ereg_replace("%20", "+", $data);
  } else {
    echo "<b>Missing XML Query &#151; Use form (POST) or querystring
(GET)</b>"; exit;

  }
  // Check for presence of NextArgs, PrevArgs and
  // append to $data
  if ($_GET['xargs'] ) {
    $data .= "&xargs=" . $_GET['xargs'] ; 
  } 


  // Create a connection to the remote XML feed
  // Read data into $xmlFeed variable
  $xmlDoc .= "?mkt=uk&adultFilter=clean&Partner=bestsearchengine_xml_uk_searchbox_test&maxCount=10&Keywords=$data";
  
  $xmlConn = fopen ($xmlDoc, "r");
  if (!$xmlConn) {
    echo "<b>Connection to XML feed could not be established</b>"; exit;
  } else {
//
// Start of code added by ForLinux
//
    $xmlFeed = "";
    do {
	$data = fread($xmlConn, 20000);
        if (strlen($data) == 0) {
          break;
        }
	$xmlFeed .= $data;
    } while ( true );
//
// End of code added by ForLinux
//
  }
  fclose($xmlConn);

  // Parse XML results into two arrays
  $parser = xml_parser_create();
  xml_parse_into_struct($parser,$xmlFeed,$values,$tags);
  xml_parser_free($parser);

  // Read listings
  $listings = array();
  $listingIndex = 0;

  foreach ($tags['LISTING'] as $key=>$val) {
    if( 'open' == $values[$val]['type'] ) {
      $listings[$listingIndex] = $values[$val]['attributes'];
      $listingIndex++;
    }
  }

  // If no listings, then we are done
  $totalListings = sizeof( $listings );
  if( 0 == $totalListings ) {
    echo '</body></html>';
    exit;
  }

  // Get one clickUrl per listing
  $listingIndex = 0;
  $totalUrls = sizeof( $tags['CLICKURL'] );
  $urlsPerListing = $totalUrls / $totalListings;

  for( $urlIndex = 0; $urlIndex < $totalUrls; $urlIndex += $urlsPerListing ) {
    $url = $values[$tags['CLICKURL'][$urlIndex]]['value'];
    $listings[$listingIndex]['CLICKURL'] = $url;
    $listingIndex++;
  }

  // Read NextArgs/PrevArgs for navigation
  $nextargs = $values[$tags['NEXTARGS'][0]]['value'];
  $prevargs = $values[$tags['PREVARGS'][0]]['value'];
  
?>

#####The code above gathers the data and the code below displays it ########


<?php
			
			// Display the results
  echo '<table align="default" border="0" width="710" cellpadding="0"><tr><td>';
    foreach ($listings as $key=>$val) {
  echo '<table align="default" width="710" height="55" cellpadding="0"
border="0" cellspacing="0"><tr> <td colspan="2" bgcolor="#E6EAEC">';
    echo '<b>';
	echo '<Span style="font-size: 8.75pt; color: #660000; font-family: arial">';
	echo $val['RANK'];
	 echo '.&nbsp;</span><a href="';
    echo $val['CLICKURL'];
    echo '"target="_blank" SPAN style="font-size: 10.5pt; color:
#1C3899; font-family: arial; text-decoration: underline" 
onclick="dmclick=1;">';
	echo $val['TITLE'];
    echo '</a></span></b><br>';
	echo '<Span style="font-size: 8pt; font-family: arial"><a href="';
	echo $val['CLICKURL'];
    echo '"target="_blank" SPAN style="font-size: 8.5pt; color:
#333366; font-family: arial; font-weight: normal; text-decoration:
none">';
	echo $val['DESCRIPTION'];
    echo '</a></span></b>';
    echo '<em>';
	echo '<font color="#000066; font-family: arial"><br><a href="';
    echo $val['CLICKURL'];
    echo '"target="_blank" onclick="dmclick=1;">';
    echo '<img src="http://www.bestsearchengine.co.uk/images/button12.gif"
border="0" align="right">';
	 echo '</a></em>';
	 
	 echo '<a href="';
    echo $val['CLICKURL'];
    echo '"target="_blank" SPAN style="font-size: 9pt; color: #000099;
font-family: arial; font-style: italic; font-weight: normal;
text-decoration: none" onclick="dmclick=1;">';
    echo $val['SITEHOST'];
	echo '</a></span> </em>';
	
	echo '<SPAN style="font-size: 9pt; color: #990000; font-family:
arial; font-style: italic; font-weight: normal; text-decoration: none"
onclick="dmclick=1;">';
	
	 echo '&nbsp; &nbsp; sponsored results</a></span> </em>';
	 
   if( 'true' == $val['BIDDEDLISTING'] ) {
      echo ' <font size="2"></font>';
	  echo '</tr> <br> </table>'; }
    }
    echo '</p>';
  echo '</td></tr>';
  
  if( $nextargs || $prevargs ) {
    echo '<tr><td align="CENTER">';
	echo '<font size="1">';
    if( $prevargs ) {
      echo '<a href="index_links_overture.php?';
      echo $prevargs;
      echo '">Last Page</a>';
    }
	echo '</font>';
    if( $prevargs && $nextargs ) {
      echo ' - ';
    }
	echo '<font size="1">';
    if( $nextargs ) {
      echo '<a href="index_links_overture.php?';
      echo $nextargs;
      echo '">Next Page</a>';
    }
    echo '</td></font></tr>';
  }
  echo '</table>';
?>
Answer  
Subject: Re: Need code to display message when no results are found in XML feed.
Answered By: sycophant-ga on 12 Aug 2006 05:03 PDT
Rated:5 out of 5 stars
 
Hi, 

The code is fairly straightforward, so this change is fairly easy. 

To change what happens when there are no results found edit this
portion of the code:

// If no listings, then we are done
$totalListings = sizeof( $listings );
if( 0 == $totalListings ) {
	echo '</body></html>';
	exit;
}


This code essentially checks the number of listings, and if it is zero
then it sends closing HTML code, and exits the script.

Change the "echo '</body></html>';" line to something else, for example:

$totalListings = sizeof( $listings );
if( 0 == $totalListings ) {
	echo 'Sorry, no resutlts were found. Please try another search term.';
	exit;
}

It shouldn't be necessary to including the closing HTML code in there,
as it looks from the code as if this script is included in another
page, so the HTML document should be closed in that.

Also, the following line, on Line 98 or so, is unnecessary and
actually appears in the output of the script.

#####The code above gathers the data and the code below displays it ########

To fix this you can do one of three things... 
1) Delete it (it's just a comment, it doesn't affect the program's operation)
2) Remove the '?>' above and '<?php' below it. It will be evaluated as
PHP and ignored (as it should be).
3) Put "<!-- " in front and " -->" behind it to make it an HTML
comment. It will no longer be displayed.

Let me know if you have any problems with this. 

Regards,
Sycophant

Request for Answer Clarification by nicnack23-ga on 13 Aug 2006 09:29 PDT
Hi, thanks for your response and I can now see where the text should
go, however I am getting this error when I search for a term which has
no advertiser.

"Warning: Invalid argument supplied for foreach() in
/home/bsadmin/public_html/mysharedaccounts/fsadministrator/index_links_overture.php
on line 89 please try another search"

Is there a way the text can appear in place of the results instead of
on a blank page.

Thanks

Clarification of Answer by sycophant-ga on 13 Aug 2006 15:38 PDT
Hi, 
In my copy of the code, copy and pasted from here, the 'Foreach' line
seems to be on line 67, and reads as follows:

foreach ($tags['LISTING'] as $key=>$val) {

To fix this the easiest thing might be to add a conditional to that
portion of code, so it reads like this:

if (is_array($tags['LISTING'])) {
  foreach ($tags['LISTING'] as $key=>$val) {
    if( 'open' == $values[$val]['type'] ) {
      $listings[$listingIndex] = $values[$val]['attributes'];
        $listingIndex++;
    }
  }
}

To make the code appear in place of the search output, do the following:
// If no listings, then we are done
$totalListings = sizeof( $listings );
if( 0 == $totalListings ) {
  ?>
<HTML CODE>
  <?php
  exit;
}

Where it says <HTML CODE> you can include any HTML code at all, so you
can create a more stylised error result to match the style of the
page.

Regards,
Sycophant
nicnack23-ga rated this answer:5 out of 5 stars and gave an additional tip of: $3.00
Excellent response time, clear and concise answers. thanks

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy