我試圖在PHP返回語句中放置天氣預報函數的結果。要使用串聯來完成此操作,我需要在代碼中移動一個foreach循環,但我無法使其工作。在返回語句之前移動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. */
}
這是我需要在返回語句之前放置的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 ?>
非常感謝您的幫助!
什麼問題,你不能關閉'?>'並打開'<?php'解析器或複製粘貼? – Shef