$php_mysql$ Posted October 3, 2010 Share Posted October 3, 2010 why is it that when i run it on local host in the browser it prints the codes like xpath("/xml_api_reply/weather/forecast_information"); $current = $xml->xpath("/xml_api_reply/weather/current_conditions"); $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions"); ?> city['data']; ?> Today's weather weather temp_f['data'] ?>° F, condition['data'] ?> Forecast weather day_of_week['data']; ?> low['data'] ?>° F - high['data'] ?>° F, condition['data'] ?> it do not wish to show output <? $xml = simplexml_load_file('http://www.google.com/ig/api?weather=jakarta'); $information = $xml->xpath("/xml_api_reply/weather/forecast_information"); $current = $xml->xpath("/xml_api_reply/weather/current_conditions"); $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions"); ?> <html> <head> <title>Google Weather API</title> </head> <body> <h1><?= print $information[0]->city['data']; ?></h1> <h2>Today's weather</h2> <div class="weather"> <img src="<?= 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?> <span class="condition"> <?= $current[0]->temp_f['data'] ?>° F, <?= $current[0]->condition['data'] ?> </span> </div> <h2>Forecast</h2> <? foreach ($forecast_list as $forecast) : ?> <div class="weather"> <img src="<?= 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?> <div><?= $forecast->day_of_week['data']; ?></div> <span class="condition"> <?= $forecast->low['data'] ?>° F - <?= $forecast->high['data'] ?>° F, <?= $forecast->condition['data'] ?> </span> </div> <? endforeach ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 3, 2010 Share Posted October 3, 2010 You should stop using the short <? and <?= tags, and use the full <?php tag syntax. Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted October 3, 2010 Author Share Posted October 3, 2010 what is wrong with this code can you tell me? <?php if(isset($_POST['submit'])) { $city_town_name = $_POST['$city_town_name']; $placename = '$city_town_name'; // city where you want local weather $lang = 'en'; $place=urlencode($placename); $place = utf8_encode($place); $url = 'http://www.google.com/ig/api?weather='.$place.',$&hl='.$lang.''; $ch = curl_init(); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt ($ch, CURLOPT_TIMEOUT, 60); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); $raw_data = curl_exec ($ch); curl_close ($ch); $xml = simplexml_load_string($raw_data); $condition = $xml->weather->current_conditions->condition['data']; $temp_c = $xml->weather->current_conditions->temp_c['data']; $humidity = $xml->weather->current_conditions->humidity['data']; $icon = $xml->weather->current_conditions->icon['data']; echo ("<h1>Local weather for $placename</h1>"); for ($i = 0; $i < count($xml->weather->forecast_conditions); $i++){ $data = $xml->weather->forecast_conditions[$i]; $day_of_week = $data->day_of_week['data']; $low = $data->low['data']; $high = $data->high['data']; $condition = $data->condition['data']; $day_of_week = utf8_decode($day_of_week); $img = 'http://img0.gmodules.com/' . $data->icon['data']; echo ("$day_of_week<br/><img src=\"$img\"/>$low°|$high°<br/>$condition<br/><br/>"); } } ?> <form action='weather.php' method='POST'/> <input name='city_town_name' type='text' value=''/> <input type='submit' name='submit' value='get weather'/> </form> i get Notice: Undefined index: $city_town_name in C:\wamp\www\fiunc\weather.php on line 4 Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 3, 2010 Share Posted October 3, 2010 $city_town_name = $_POST['city_town_name']; //was $_POST['$city_town_name'] Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 3, 2010 Share Posted October 3, 2010 Does these lines really make sense to you? Hint: they shouldn't . . . $city_town_name = $_POST['$city_town_name']; $placename = '$city_town_name'; // city where you want local weather Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted October 3, 2010 Author Share Posted October 3, 2010 lol happened for copy paste thanks :-) ok another issue now when i submit no results show? Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted October 3, 2010 Author Share Posted October 3, 2010 actually they do not make any im just for time being keeping it like that Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 3, 2010 Share Posted October 3, 2010 $placename = $city_town_name; // city where you want local weather variables aren't interpolated in single quote ('') strings, they need to be double quoted ("") for the variable to be replaced by it's value. It's not neccessary for it to be quoted at all in this case though as you are simply assigning one variable's value to another. Although, as Pikachu2000 mentioned, you should really be doing this: $placename = $_POST['$city_town_name']; // city where you want local weather Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted October 3, 2010 Author Share Posted October 3, 2010 those are fixed :-) but now is displaying after submiting i get blank page tho when i print the $city_town_name i get the result from what i submited using form. but rest nothing is displaying Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 3, 2010 Share Posted October 3, 2010 Show the code as it currently stands. Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted October 3, 2010 Author Share Posted October 3, 2010 this is how it looks <?php if(isset($_POST['submit'])) { $city_town_name = $_POST['city_town_name']; $placename = '$city_town_name'; // city where you want local weather $lang = 'en'; $place=urlencode($placename); $place = utf8_encode($place); $url = 'http://www.google.com/ig/api?weather='.$place.',$&hl='.$lang.''; $ch = curl_init(); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt ($ch, CURLOPT_TIMEOUT, 60); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); $raw_data = curl_exec ($ch); curl_close ($ch); $xml = simplexml_load_string($raw_data); $condition = $xml->weather->current_conditions->condition['data']; $temp_c = $xml->weather->current_conditions->temp_c['data']; $humidity = $xml->weather->current_conditions->humidity['data']; $icon = $xml->weather->current_conditions->icon['data']; echo ("<h1>Local weather for $placename</h1>"); for ($i = 0; $i < count($xml->weather->forecast_conditions); $i++){ $data = $xml->weather->forecast_conditions[$i]; $day_of_week = $data->day_of_week['data']; $low = $data->low['data']; $high = $data->high['data']; $condition = $data->condition['data']; $day_of_week = utf8_decode($day_of_week); $img = 'http://img0.gmodules.com/' . $data->icon['data']; echo ("$day_of_week<br/><img src=\"$img\"/>$low°|$high°<br/>$condition<br/><br/>"); } } ?> <form action='weather.php' method='POST'/> <input name='city_town_name' type='text' value=''/> <input type='submit' name='submit' value='get weather'/> </form> after submit the weather data does not display Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 3, 2010 Share Posted October 3, 2010 You didn't make the change I told you too... <?php if(isset($_POST['submit'])) { $placename = $_POST['city_town_name']; // city where you want local weather $lang = 'en'; $place=urlencode($placename); $place = utf8_encode($place); $url = 'http://www.google.com/ig/api?weather='.$place.',$&hl='.$lang.''; $ch = curl_init(); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt ($ch, CURLOPT_TIMEOUT, 60); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); $raw_data = curl_exec ($ch); curl_close ($ch); $xml = simplexml_load_string($raw_data); $condition = $xml->weather->current_conditions->condition['data']; $temp_c = $xml->weather->current_conditions->temp_c['data']; $humidity = $xml->weather->current_conditions->humidity['data']; $icon = $xml->weather->current_conditions->icon['data']; echo ("<h1>Local weather for $placename</h1>"); for ($i = 0; $i < count($xml->weather->forecast_conditions); $i++){ $data = $xml->weather->forecast_conditions[$i]; $day_of_week = $data->day_of_week['data']; $low = $data->low['data']; $high = $data->high['data']; $condition = $data->condition['data']; $day_of_week = utf8_decode($day_of_week); $img = 'http://img0.gmodules.com/' . $data->icon['data']; echo ("$day_of_week<br/><img src=\"$img\"/>$low°|$high°<br/>$condition<br/><br/>"); } } ?> <form action='weather.php' method='POST'/> <input name='city_town_name' type='text' value=''/> <input type='submit' name='submit' value='get weather'/> </form> Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted October 3, 2010 Author Share Posted October 3, 2010 now done it i put the variables inside " quotes. thanks soo much for help. off to sleep already dawn here :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.