Hi, I'm getting a couple of warnings when I run my script. I was wondering if someone could fix these for me. I'm not that good at PHP, so I'm having trouble finding what's wrong.

The warnings are:

Notice: Undefined offset: 4 in C:\Sites\Single42\litohin\webroot\nwsforecast.php on line 143

Notice: Undefined offset: 4 in C:\Sites\Single42\litohin\webroot\nwsforecast.php on line 144

Here is the code being used:

<?php

// Force Defaults for Zone and County
if ( ! isset($REQUEST['warnzone']) )
$
REQUEST['warnzone']="TXZ081";
if ( ! isset($REQUEST['warncounty']) )
$
REQUEST['warncounty']="TXC043";
if ( ! isset($REQUEST['warnlocal']) )
$
REQUEST['warnlocal']="Rural+Big+Bend+Area";

//You can pass data to this script with:
// http://www.carterlake.org/testadvisory.php?warnzone=IAZ069&warncounty=IAC155&warnlocal=Carter+Lake
//Where the zone is your zone and the county is your county and location is your location
//using pluses in place of spaces

$warnzone = $REQUEST['warnzone'];
$warncounty = $
REQUEST['warncounty'];
$warnlocal = $_REQUEST['warnlocal'];

//import NOAA Advisory info
//data can be altered by changing the zone and county numbers
//Target data ends up in $targetwarn and $targettext[0]

$html = implode('', file("http://www.crh.noaa.gov/showsigwx.php?warnzone=TXZ081&warncounty=TXC043"));

//Get the advisory headers and put them in an array

	preg_match_all('|<h3>(.*)</h3>|', $html, $headers);
	$warnheaders = $headers[1];

//Get the advisory text and put them into an array as well

	preg_match_all('|<pre>(.*)</pre>|Uis', $html, $headers);
	$warntext = $headers[1];

//If there is more than one advisory, we need to set its priority

if (count($warnheaders) >= 1) {

$i = 0;
$flag = 0;

//First, around here tornados are the biggest danger. A warning is critical information.
//Display this one first no matter what!

while ($i < count($warnheaders)):
	if (preg_match("/Tornado Warning/i", $warnheaders[$i])) {  
		$targetwarn = $warnheaders[$i];
		$targettext = $warntext[$i];
		$targettext = explode("$$",$targettext);
		$flag = 1;
		break;
	}
	$i++;
endwhile;

//Next if there are none of the above found. Display the first warning message.

if ($flag == 0) {
	$i = 0;
	while ($i < count($warnheaders)):
		if (preg_match("/Warning/i", $warnheaders[$i])) {  
		$targetwarn = $warnheaders[$i];
		$targettext = $warntext[$i];
		$targettext = explode("$$",$targettext);
			$flag = 1;
			break;
		}
		$i++;
	endwhile;
}

//Next if there are none of the above found. Display the first watch message.

if ($flag == 0) {
	$i = 0;
	while ($i < count($warnheaders)):
		if (preg_match("/Watch/i", $warnheaders[$i])) {  
		$targetwarn = $warnheaders[$i];
		$targettext = $warntext[$i];
		$targettext = explode("$$",$targettext);
			$flag = 1;
			break;
		}
		$i++;
	endwhile;
}

//Next if there are none of the above found. Display the first advisory message.

if ($flag == 0) {
	$i = 0;
	while ($i < count($warnheaders)):
		if (preg_match("/Advisory/i", $warnheaders[$i])) {  
		$targetwarn = $warnheaders[$i];
		$targettext = $warntext[$i];
		$targettext = explode("$$",$targettext);
			$flag = 1;
			break;
		}
		$i++;
	endwhile;
}

//Next if there are none of the above found. Display the first statement message.

if ($flag == 0) {
	$i = 0;
	while ($i < count($warnheaders)):
		if (preg_match("/Statement/i", $warnheaders[$i])) {  
		$targetwarn = $warnheaders[$i];
		$targettext = $warntext[$i];
		$targettext = explode("$$",$targettext);
			$flag = 1;
			break;
		}
		$i++;
	endwhile;
}

//Next if there are none of the above found. Set the advisory to default message.

if ($targetwarn == "Hazardous Weather Outlook") {
	$targetwarn = "NO CURRENT ADVISORIES";
	$targettext[0] = "THERE ARE NO ACTIVE WATCHES, WARNINGS OR ADVISORIES";
} else if ($targetwarn == "No Active Hazardous Weather Conditions Found") {
	$targetwarn = "NO CURRENT ADVISORIES";
	$targettext[0] = "THERE ARE NO ACTIVE WATCHES, WARNINGS OR ADVISORIES";
} else if (empty($targetwarn)) {
	$targetwarn = "NO CURRENT ADVISORIES";
	$targettext[0] = "THERE ARE NO ACTIVE WATCHES, WARNINGS OR ADVISORIES";
} else if ($targetwarn == "Short Term Forecast") {
	$targetwarn = "NO CURRENT ADVISORIES";
	$targettext[0] = "THERE ARE NO ACTIVE WATCHES, WARNINGS OR ADVISORIES";
}

if ($targetwarn <> "NO CURRENT ADVISORIES") {

$warnlist = 'ALL CURRENT ADVISORIES:';


	for ($i = 0; $i <= count($warnheaders); $i++) {
    		$warnheaderplus = preg_replace( '| |', '+', $warnheaders[$i] );
    		$warnlist = $warnlist . '<br><a href="http://www.crh.noaa.gov/showsigwx.php?warnzone=' . $warnzone. '&warncounty=' . $warncounty . '&local_place1=' . $warnlocal . '&product1=' . $warnheaderplus . '" target="_new">' . $warnheaders[$i] . '</a>';
	}
}

} error_reporting(0);

echo "<b>";
echo $targetwarn;
echo "</b><br><br><pre>";
echo $targettext[0];
echo $warnlist;
echo "</pre>";
?>

    At line 142, I think you want to change the "<=" to "<", since the array $warnheaders presumably starts with index 0:

    for ($i = 0; $i < count($warnheaders); $i++) {
    

    Alternatively, so you don't have to worry about which index it starts with or how many elements there are, you could use a foreach loop instead:

    foreach($warnheaders as $i => $value) {
    

      Thanks the foreach loop worked.

        I have one last question. How would I go about display all the advisories instead of just one? The code outputs as show by following the below link, but I don't want a link to another website with the rest of the advisories.

        http://www.cwmro.com/nwsforecast.php

          I'm getting a new error now:

          Notice: Undefined variable: targetwarn in C:\Sites\Single42\litohin\webroot\nws.php on line 552

          Notice: Undefined variable: targetwarn in C:\Sites\Single42\litohin\webroot\nws.php on line 555

          Could someone help me with these?

            Write a Reply...