$php_mysql$ Posted October 5, 2010 Share Posted October 5, 2010 im getting Undefined offset: 0 in many line what can be the issue? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><!-- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> --> <title>Untitled Document</title> </head> <body> <?php if(isset($_POST['submit'])) { $userquery = $_POST['userquery']; $xml = simplexml_load_file("http://www.google.com/ig/api?weather='".$userquery."'"); $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"); echo "<h1>".$information[0]->city['data'].", ".$information[0]->forecast_date['data']."</h1>"; echo "<h2>Current Contition</h2>"; echo "<div class=\"weather\">"; echo "<table width='250' border='1'>"; echo "<tr>"; echo "<td><img src='http://www.google.com".$current[0]->icon['data']."' width='70' height='70' alt='weather'></td>"; echo "<td>"; echo $current[0]->condition['data']; echo "<br/>Min "; echo $current[0]->temp_c['data']."° C, <br/>"; echo $current[0]->temp_f['data']."° F, <br/>"; echo $current[0]->humidity['data'].", <br/>"; echo $current[0]->wind_condition['data']."."; echo "</td>"; echo "</tr>"; echo "</table>"; echo "</span>"; echo "</div>"; echo "<h2>Forecast</h2>"; foreach ($forecast_list as $forecast) : echo "<div class=\"weather\">"; echo "<table width='250' border='1'>"; echo "<tr>"; echo "<td>"; echo "<img src='http://www.google.com".$forecast->icon['data']."' width='50' height='50'alt='weather'>"; echo "</td>"; echo "<td>"; echo "<div>".$forecast->day_of_week['data']."</div>"; echo "<span class=\"condition\">"; echo $forecast->low['data']."° F - ".$forecast->high['data']."° F,<br/>"; echo $forecast->condition['data']."."; echo "</td>"; echo "</tr>"; echo "</table>"; echo "</span>"; echo "</div>"; endforeach; } ?> <form action="test.php" method="post"> <input name="userquery" type="text" value=""/> <input type="submit" name='submit' value="get"/> </form> </body> </html> here all errors and there are few more Notice: Undefined offset: 0 in C:\wamp\www\fiunc\test.php on line 16 Notice: Trying to get property of non-object in C:\wamp\www\fiunc\test.php on line 16 Notice: Undefined offset: 0 in C:\wamp\www\fiunc\test.php on line 16 Notice: Trying to get property of non-object in C:\wamp\www\fiunc\test.php on line 16 , it happens when i add if(isset($_POST['submit'])) { $userquery = $_POST['userquery']; and this in '".$userquery."' $xml = simplexml_load_file("http://www.google.com/ig/api?weather='".$userquery."'"); Link to comment https://forums.phpfreaks.com/topic/215201-undefined-offset-error-in-many-lines/ Share on other sites More sharing options...
phpchamps Posted October 5, 2010 Share Posted October 5, 2010 You will get these notices if google is not returning the whether information. because in your code you have not checked if that element is exists then only show it or only if the return xml from google is right then only execute code... you can try "Delhi" (without quotes) in your post data and you will not get notice because this time google will return successful xml. Link to comment https://forums.phpfreaks.com/topic/215201-undefined-offset-error-in-many-lines/#findComment-1119235 Share on other sites More sharing options...
$php_mysql$ Posted October 5, 2010 Author Share Posted October 5, 2010 wow actually u r right it does i was mistyping so it gave error. tell me please how do i check if google is returning a results? Link to comment https://forums.phpfreaks.com/topic/215201-undefined-offset-error-in-many-lines/#findComment-1119236 Share on other sites More sharing options...
phpchamps Posted October 5, 2010 Share Posted October 5, 2010 according to be best way would be create a XSD file which will validate the response based on that show it. Link to comment https://forums.phpfreaks.com/topic/215201-undefined-offset-error-in-many-lines/#findComment-1119239 Share on other sites More sharing options...
$php_mysql$ Posted October 5, 2010 Author Share Posted October 5, 2010 could it not be done something like if($userquery == "1") show results } else{ error? } Link to comment https://forums.phpfreaks.com/topic/215201-undefined-offset-error-in-many-lines/#findComment-1119244 Share on other sites More sharing options...
$php_mysql$ Posted October 5, 2010 Author Share Posted October 5, 2010 anyone could help on how i add i error message it no values returns from google? Link to comment https://forums.phpfreaks.com/topic/215201-undefined-offset-error-in-many-lines/#findComment-1119332 Share on other sites More sharing options...
$php_mysql$ Posted October 5, 2010 Author Share Posted October 5, 2010 i tried $fp = @fopen($xml,"r"); if(!$fp) { echo"Cannot Connect<br/>"; echo"Try Again Later<br/>"; } else { but then everythin disappears and i only get to see echo"Cannot Connect<br/>"; echo"Try Again Later<br/>"; what other way could i check if results being sent? Link to comment https://forums.phpfreaks.com/topic/215201-undefined-offset-error-in-many-lines/#findComment-1119416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.