...

View Full Version : Problem with define/cannot get Celcius to display in weather script



Errica
03-05-2007, 04:40 AM
I have two php pages, the first of which contains a "config section." In the config section of the first page, I'd like to define (toggle between) Fahrenheit and Celsius.

Can someone help?



//////// PAGE 1 (index.php) ////////



<?php
include('weather.class.php');


/* Config Section */

$zip = "24018"; // Input your zip code

define('IMAGES', 'icons/sm/'); // Input your image folder location

/* End Config Section */


if($zip != '')
{
$weather = new Weather();
$weather = $weather->getWeather($zip, $_GET['units']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Weather</title>
</head>
<body>

<?php
{
echo "\t\t\tCurrent Temperature<br/>".$weather['temp']."\n";
}
?>

</body>
</html>



//////// PAGE 2 (weather.class.php) ////////



<?php
define('WEATHER_FILE', 'weather'); // prefix for caching, <zip>.xml

class weather {
var $data;
var $saved;
var $metric;

function getWeather($zip, $units='c') {
// setup the units
$this->metric = (strtoupper($units) == 'C');
if($this->metric) $units = array(temp=>'C', distance=>'km', measure=>'mb', speed=>'kph');
else $units = array(temp=>'F', distance=>'mi', measure=>'in', speed=>'mph');
$pdir = array('steady', 'rising', 'falling');
$wdir = array('N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N');

// get the feed contents from file unless older than 1 hour
$file = WEATHER_FILE.".{$zip}.xml";
if(!file_exists($file) || filemtime($file) < time() - 3600) {
$this->data = @file_get_contents("http://xml.weather.yahoo.com/forecastrss?p={$zip}&u=".$units['temp']);
$fp = @fopen($file, 'w');
@fwrite($fp, $this->data);
@fclose($fd);
}
else $this->data = @file_get_contents($file);
if(strlen($this->data) <= 0) return;

// get the units of the saved file
$this->saved = explode('"', $this->tag('yweather:units'));
$this->saved = (strtoupper($this->saved[1]) == 'C');

$return = array();

// get the location
$attr = explode('"', $this->tag('yweather:location'));
$return['location'] = $attr[1].', '.$attr[3].', '.$attr[5];

// get the wind data
$attr = explode('"', $this->tag('yweather:wind'));
$return['windchill'] = $this->convert($attr[1], 'temp').'&deg;'.$units['temp'];
$return['wind'] = $this->convert($attr[5], 'speed').' '.$units['speed'].' '.$wdir[round($attr[3]/45)];

// get the atmosphere data
$attr = explode('"', $this->tag('yweather:atmosphere'));
$return['humidity'] = $attr[1].'%';
$return['visibility'] = ($this->convert($attr[3], 'distance') / 100).' '.$units['distance'];
$return['preasure'] = $this->convert($attr[5], 'measure').' '.$units['measure'].' '.$pdir[$attr[7]];

$attr = explode('"', $this->tag('yweather:astronomy'));
$return['sunrise'] = $attr[1];
$return['sunset'] = $attr[3];

// get the temperature data
$attr = explode('"', $this->tag('yweather:condition'));
$return['text'] = $attr[1];
$return['temp'] = $this->convert($attr[5], 'temp').'&deg;'.$units['temp'];
$return['image'] = IMAGES.$this->translate($attr[3], 'temp').'.png';

// get the two forecasts
$return['forecast'] = array();
for($i = 0; $i < 2; $i++) {
$attr = explode('"', $this->tag('yweather:forecast',$i));
if(count($attr) > 1) {
$day = array();
$day['when'] = $attr[1];
$day['low'] = $this->convert($attr[5], 'temp').'&deg;'.$units['temp'];
$day['high'] = $this->convert($attr[7], 'temp').'&deg;'.$units['temp'];
$day['text'] = $attr[9];
$day['image'] = IMAGES.$this->translate($attr[11]).'.png';
array_push($return['forecast'], $day);
}
}
return $return;
}

function tag($tag, $skip=0) {
$start = -1;
for($i = 0; $i <= $skip; $i++)
$start = strpos($this->data, "<{$tag}", $start + 1);
if($start === false) return false;
$start += strlen($tag) + 1;
$end = strpos($this->data, "</{$tag}>", $start);
if($end === false)
$end = strpos($this->data, '/>', $start);
return trim(substr($this->data, $start, $end - $start));
}

function convert($value, $type) {
switch($type) {
case 'temp': // Celsius or Farenheit
if($this->saved == $this->metric) return $value;
if($this->saved) return number_format($value * 1.8 + 32, 0);
return number_format(($value - 32) / 1.8, 0);
case 'speed': // kilometers per hour or miles per hour
case 'distance': // kilometers or miles
if($this->saved == $this->metric) return $value;
if($this->saved) return number_format($value * 0.621371192, 0);
return number_format($value * 1.609344, 0);
case 'measure': // millibars or inches
if($this->saved == $this->metric) return $value;
if($this->saved) return number_format($value * 0.0295301, 2);
return number_format($value * 33.8637526, 0);
}
}
}
?>

koyama
03-05-2007, 11:48 PM
It seems like you have set up the page so that:

index.php?units=C // shows temperature in celsius
index.php?units=F // shows temperature in fahrenheit

So is this working? Or is it the toggle functionality itself you are asking about?

If it is the latter, what kind of toggle are you imagining?

Inigoesdr
03-06-2007, 12:36 AM
You're including the class in the index file, right?
If the zip is empty you will either get no text for the "Current Temp" or more likely an error. You should force either a default zip code or be sure the user enters one.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum