我试图将天气预报功能的结果放在PHP返回语句中。要使用连接来执行此操作,我需要在代码中移动foreach循环,我无法使其工作。
我正在尝试创建这样的函数::
function getWeatherForecast($atts) {
all variables go here;
return 'concatenated output';
}
这是指向完整脚本的链接:http://pastebin.com/fcQpskmy
这就是我现在所拥有的:
function getWeatherForecast($atts) {
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=barcelona');
$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");
/* The foreach loop should go here */
return '<h2>The weather today ' . $information[0]->city['data'] . '</h2>';
/* etc. etc. */
}
这是我需要在return语句之前放置的foreach循环:
<?php foreach ($forecast_list as $forecast) : ?>
<div>
<img src="<?php echo 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?>
<div><?php echo $forecast->day_of_week['data']; ?></div>
<span>
<?php echo $forecast->low['data'] ?>° F – <?php echo $forecast->high['data'] ?>° F,
<?php echo $forecast->condition['data'] ?>
</span>
</div>
<?php endforeach ?>
非常感谢你的帮助!!
答案 0 :(得分:0)
我们很清楚,你希望foreach能够在函数内部吗?
在那种情况下:
$html_code = "";
foreach ($forecast_list as $forecast) {
$html_code .='<div>
<img src="http://www.google.com' . $forecast->icon['data']. '" alt="weather" />
<div>'.$forecast->day_of_week['data'].'</div>
<span>
'. $forecast->low['data'] .'° F – '. $forecast->high['data'] .'° F,
'. $forecast->condition['data'] .'
</span>
</div>
}
然后$ html_code将需要在return语句中连接所有html代码。
这是你需要的吗?
答案 1 :(得分:0)
我试过这个并且有效:
<?
echo getWeatherForecast('');
function getWeatherForecast($atts) {
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=barcelona');
$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");
foreach ($forecast_list as $forecast) {
?>
<div>
<img src="<?php echo 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?>
<div><?php echo $forecast->day_of_week['data']; ?></div>
<span>
<?php echo $forecast->low['data'] ?>° F – <?php echo $forecast->high['data'] ?>° F,
<?php echo $forecast->condition['data'] ?>
</span>
</div>
<? }
return '<h2>The weather today ' . $information[0]->city['data'] . '</h2>';
/* etc. etc. */
}
?>
但这不是正确的方法。使用一个变量来累积html,然后返回所有的html。像这样:
<?
echo getWeatherForecast('');
function getWeatherForecast($atts) {
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=barcelona');
$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 = '';
foreach ($forecast_list as $forecast) {
$html .= '<div>';
$html .= '<img src="http://www.google.com'.$forecast->icon['data'].'" alt="weather"/>';
$html .= '<div>'.$forecast->day_of_week['data'].'</div>';
$html .= '<span>';
$html .= $forecast->low['data'].'° F – '.$forecast->high['data'].'° F,'.$forecast->condition['data'];
$html .= '</span>';
$html .= '</div>';
}
$html .= '<h2>The weather today ' . $information[0]->city['data'] . '</h2>'
return $html;
/* etc. etc. */
}
?>