Jump to content

results not showing in browser?


$php_mysql$

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/215075-results-not-showing-in-browser/
Share on other sites

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

$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

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

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>

 

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.