Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2005
    Posts
    252
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Problem with define/cannot get Celcius to display in weather script

    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 Code:
    <?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 Code:
    <?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 320);
                return 
    number_format(($value 32) / 1.80);
            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.6213711920);
                return 
    number_format($value 1.6093440);
            case 
    'measure'// millibars or inches
                
    if($this->saved == $this->metric) return $value;
                if(
    $this->saved) return number_format($value 0.02953012);
                return 
    number_format($value 33.86375260);
            }
        }
    }
    ?>
    Last edited by Errica; 03-05-2007 at 03:36 PM.

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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?

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •