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
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.

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.