cheffy cheffy - 12 days ago 8
PHP Question

If my webpage is blank do this

I have a page on my website that is fed from an RSS feed for weather alerts. If there is an alert if will display information, if not it will be empty. The link for this page is,

http://micklehamweather.com/rss-directbomvicalert.php

Based on a previous question I have asked here I have found out this page returns "\n" when the RSS feed is blank (rather than a blank page)

On my main page I trying to write some code to look at this page and if it is empty display one image, if not display a different image.

The code I currently have is below, but I can't seem to get it to work,

<?php
if ("http://micklehamweather.com/rss-directbomvicalert.php" == "\n") {
echo' Alerts <img width="20" height="20" style="vertical-align: middle;" alt="Warnings issued by the Bureau of Meteorology" src="images/alert-icon-120.png" border="0"> Warnings <img src="/green_alert.png" width="12" height="12">&nbsp;&nbsp;&nbsp';
}
else {
echo' Alerts <img width="20" height="20" style="vertical-align: middle;" alt="Warnings issued by the Bureau of Meteorology" src="images/alert-icon-120.png" border="0"> Warnings <img src="/red_alert.gif" width="12" height="12">&nbsp;&nbsp;&nbsp';
}
?>


Does anybody know what I am doing wrong or have any suggestions that would help?

I have added the code for the RSS page below

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<link type="text/css" href="rss-style.css" rel="stylesheet">

<body bgcolor="#FFE991">

<?php
require_once("rsslib.php");
$url = "http://www.bom.gov.au/fwo/IDZ00055.warnings_nt.xml";
echo RSS_Display($url, 15, false, false);
?>
</fieldset>
</body>
</html>


Many thanks.

As per my note below. Copy of the error messages at the top of the page when the RSS feed fails,

Warning: DOMDocument::load(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /homepages/13/d548979659/htdocs/rsslib.php on line 77

Warning: DOMDocument::load(http://www.bom.gov.au/fwo/IDZ00059.warnings_vic.xml): failed to open stream: php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /homepages/13/d548979659/htdocs/rsslib.php on line 77

Answer

Try something like this:

<?php
    require_once("rsslib.php");
    $url = "http://www.bom.gov.au/fwo/IDZ00055.warnings_nt.xml";

    $rss_result = RSS_Display($url, 15, false, false); // set the result of this function to a variable rather than echoing it straight out

    echo $rss_result;  // then you can echo the variable

    // and test it:

    if (trim($rss_result) == '') // use trim too
    {
       // do green
    }
    else
    {
      // do red
    }
?>

Comments