Question

I am using yahoo weather feed and simplepie to get weather for my digital display. Since it is a website run on a screen, I can't refresh the browser all the time manually.

I am just wondering is there anything eg jquery or php doc can help me update the weather information periodically as the weather changes?? If so would you be able to give me some directions on how to implement? your help is greatly appreciated.

Here is some of my code just in case it is useful

<div id="weather">
        <div id="title">
                <ul>
                    <!--<li> Outside Temp </li>-->
                    <li> Today </li>
                    <li> Tomorrow </li>

                </ul>
        </div> <!--title-->

        <div id="forecast">

        <div id="images">
            <ul>
                <?php foreach ($weather->get_forecasts() as $forecast): ?>

                <li><img src="<?php echo $forecast->get_image(); ?>" alt="Weather" /></li>

                <?php endforeach; ?>
            </ul>
         </div> <!--images-->

        <div id="degrees">
            <ul>
                <?php foreach ($weather->get_forecasts() as $forecast): ?>

                <li><?php echo $forecast->get_low(); ?>&deg; &nbsp; <?php echo $forecast->get_high(); ?>&deg; </li>

                <?php endforeach; ?>
            </ul>
            </div><!--degrees-->

    </div> <!--forecast-->
<!--</div> <!--second-->

Some header part info:

<php

require_once('simplepie/simplepie.inc');
require_once('simplepie/simplepie_yahoo_weather.inc');

$feed = new SimplePie();

$feed->set_feed_url('http://weather.yahooapis.com/forecastrss?w=1234567&u=c'); 

$feed->set_item_class('SimplePie_Item_YWeather');

$feed->init();

$weather = $feed->get_item(0);

?>

Thank you very much!

S:)

Was it helpful?

Solution

Since you're not already using JavaScript, a really simple way for you to periodically refresh the page might be to use the meta refresh tag. Although deprecated, it might be a quick way for you to get dynamic pages. Simply add the tag to the <head> of the page:

<meta http-equiv="refresh" content="5"> <!--Refresh the page every 5 seconds-->

However, you might want to consider using ajax to load your content into the page instead. This might not be the best possible approach to this but here's some quick and dirty code.

In the main page, you could use jQuery load to periodically hit the URL for your PHP script and load the content into a div for display:

<html>
<head>
    <script type="text/javascript">
        $(document).ready(function(){
            setTimeout(loadWeather, 5000); //using setTimeout instead of a setInterval to avoid having multiple queries queue up when the first one fails to return within the timeout period
        });

        function loadWeather(){
            $('#weather').load('weather.php);
            setTimeout(loadWeather, 5000);
        }
    </script>
</head>
<body>
    <div id="weather" />
</body>
</html>

where weather.php might look something like this:

<div id="weather">
        <div id="title">
                <ul>
                    <!--<li> Outside Temp </li>-->
                    <li> Today </li>
                    <li> Tomorrow </li>

                </ul>
        </div> <!--title-->

        <div id="forecast">

        <div id="images">
            <ul>
                <?php foreach ($weather->get_forecasts() as $forecast): ?>

                <li><img src="<?php echo $forecast->get_image(); ?>" alt="Weather" /></li>

                <?php endforeach; ?>
            </ul>
         </div> <!--images-->

        <div id="degrees">
            <ul>
                <?php foreach ($weather->get_forecasts() as $forecast): ?>

                <li><?php echo $forecast->get_low(); ?>&deg; &nbsp; <?php echo $forecast->get_high(); ?>&deg; </li>

                <?php endforeach; ?>
            </ul>
            </div><!--degrees-->

    </div> <!--forecast-->
<!--</div> <!--second-->
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top