here able load div value's after time interval
but need load div values default also.(when page loads)
how ?
<script type="text/javascript"> $(document).ready(function() { setinterval(function(){ $("#temperature").html(''); $("#humidity").html(''); $("#weather").html(''); $("#pressure").html(''); $("#wind").html(''); $("#sunrise").html(''); $.ajax({ url: 'api.php', datatype: "json", type: 'get', success: function(data) { if (data) { alert('hi'); document.getelementbyid("temperature").innerhtml = data['temperature']; document.getelementbyid("humidity").innerhtml = data['humidity']; document.getelementbyid("weather").innerhtml = data['condition']; document.getelementbyid("pressure").innerhtml = data['pressure']; document.getelementbyid("wind").innerhtml = data['wind']; document.getelementbyid("sunrise").innerhtml = data['sunrise']; } } }); }, 600000); }); </script> <div id="temperature"></div> <div id="humidity"></div> <div id="weather"></div> <div id="pressure"></div> <div id="wind"></div> <div id="sunrise"></div> api.php
this api.php trying load data
<?php include('yahoo_weather_codes.php'); //error_reporting(0); $base_url = "http://query.yahooapis.com/v1/public/yql"; $yql_query = 'select * weather.forecast woeid in (select woeid geo.placefinder state="andhra pradesh" , city="amaravathi")'; $yql_query_url = $base_url . "?q=" . urlencode($yql_query) . "&format=json"; // make call curl $session = curl_init($yql_query_url); curl_setopt($session, curlopt_returntransfer,true); $json = curl_exec($session); // convert json php object $phpobj = json_decode($json); //echo '<pre>';print_r($phpobj).'<pre>'; $fahrenheit = $phpobj->query->results->channel->item->condition->temp; $celsius = round(5/9*($fahrenheit-32)); $yahoo_data = array(); $yahoo_data['temperature'] = $celsius; $yahoo_data['code'] = $phpobj->query->results->channel->item->condition->code; $yahoo_data['humidity'] =$phpobj->query->results->channel->atmosphere->humidity; $yahoo_data['condition'] =$phpobj->query->results->channel->item->condition->text; $yahoo_data['pressure'] =$phpobj->query->results->channel->atmosphere->pressure; $yahoo_data['wind'] =$phpobj->query->results->channel->wind->speed; $yahoo_data['sunrise'] =$phpobj->query->results->channel->astronomy->sunrise; $yahoo_data['sunset'] =$phpobj->query->results->channel->astronomy->sunset; $yahoo_data['date'] =$phpobj->query->results->channel->item->forecast['0']->date; $yahoo_data['city'] =$phpobj->query->results->channel->location->city; $yahoo_data['country'] =$phpobj->query->results->channel->location->country; $yahoo_data['region'] =$phpobj->query->results->channel->location->region; $yahoo_data['key'] = $phpobj->query->results->channel->item->condition->code; echo json_encode($yahoo_data); ?>
just move ajax loading separate function can use both on document load , in our interval. , since using jquery, may replace cumbersome getelementbyid equivalent jquery selectors:
<script type="text/javascript"> $(document).ready(function() { function updatedata(){ $("#temperature").html(''); $("#humidity").html(''); $("#weather").html(''); $("#pressure").html(''); $("#wind").html(''); $("#sunrise").html(''); $.ajax({ url: 'api.php', datatype: "json", type: 'get', success: function(data) { if (data) { alert('hi'); $("#temperature").html(data['temperature']); $("#humidity").html(data['humidity']); $("#weather").html(data['condition']); $("#pressure").html(data['pressure']); $("#wind").html(data['wind']); $("#sunrise").html(data['sunrise']); } } }); } updatedata(); setinterval(updatedata, 600000); }); </script>
Comments
Post a Comment