Click to See Complete Forum and Search --> : PHP maths issue... cant see the issue must be going blind


Mouse77e
11-26-2007, 07:29 AM
Need a little help with a maths issue

It has been a while since I did any PHP and I seem to have lost the plot a little here…

<?php
$xml_file=file_get_contents("http://xml.weather.yahoo.com/forecastrss?p=CHXX0049&u=c");
//******************** GETTING THE INFORMATION ****************************
// For getting the last build date
preg_match_all("#<lastBuildDate>(.*?)</lastBuildDate>#s",$xml_file,$today);
$today= $today[1][0];
// for getting the sunrise and the sunset
preg_match_all("/<yweather:astronomy([^\<]*)/i", $xml_file, $res0);
// res1 for getting the sunset and sunrise
preg_match_all("/([^\"]*)/i", $res0[1][0], $res1);
// res0 contains all the information/day
preg_match_all("/<yweather:forecast([^<]*)/i", $xml_file, $res);
//************************************************************************
$n=0;
$days=count($res); // count the number of forecast days in the array
while($n<$days)
{
preg_match_all("/\"([^\"]*)/i", $res[1][$n], $array);
$day[$n] =$array[1][0];
$date[$n]=$array[1][2];
$low[$n] =$array[1][4];
$high[$n]=$array[1][6];
$text[$n]=$array[1][8];
$code[$n]=$array[1][10];
$n++;
}
$low_f=$low[$n]*1.8+32;
$high_f=$high[$n]*1.8+32;
echo "<h1><img border=\"0\" src=\"./img/weather.gif\" height=\"25\" width=\"25\">The Weather</h1>";
echo "<table width=\"60%\" align=\"center\"><tr>";
echo "<tr><td><b>Sunrise:</b></td><td>".$res1[1][2]."</td></tr>";
echo "<tr><td><b>Sunset :</b></td><td>".$res1[1][6]."</td></tr>";
echo "</table> ";
echo "<table width=\"60%\" align=\"center\" border=\"1\"><tr>";
for($n=0; $n<$days;$n++) // Day Forecast day 1,2,3, ...
{
echo "<td align=\"center\"><h1>".$day[$n]."</h1>"; // Wed
echo $date[$n]."<br>";// 26 Oct 2005
echo "<img src=\"http://afterthemouse.com/themes/garland/images/weather/".$code[$n].".gif\" ><br>";
echo $text[$n]."<br>"; // Partly Cloudy
echo "Low °C: ".$low[$n];// 8
echo " - °F:" .$low_f."<br>";
echo "High °C: " .$high[$n];// 19
echo " - °F:" .$high_f."<br>";
echo "</td>";
}
echo "</tr></table> ";
echo "</ul>";
echo "</div>";

the code pulls in the weather from Yahoo’s service with temperatures in centigrade and I want to output the results in farenhight as well… but the lines

$low_f=$low[$n]*1.8+32;
$high_f=$high[$n]*1.8+32;

always return “32” so it doesn’t appear that the first part of the calculation is working or that that the variable being used as the starting point is always 0.

Have a look at the test page and you’ll see the dilemma… http://afterthemouse.com/node/193

Many thanks for your interest.

Znupi
11-26-2007, 08:02 AM
It's not a maths issue :). After the while, $n will be 1 too much, so you need:

$low_f=$low[$n-1]*1.8+32;
$high_f=$high[$n-1]*1.8+32;

If that still doesn't work, after the while try to echo $low[$n-1] and $high[$n-1] and see what's in them :)

Hope this helps :)