$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."'"); Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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? } Quote Link to comment 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? Quote Link to comment 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? 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.