Jump to content

Undefined offset error in many lines


$php_mysql$

Recommended Posts

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

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.

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.