2011-09-26 71 views
0

我試圖在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'] ?>&deg; F – <?php echo $forecast->high['data'] ?>&deg; F, 
    <?php echo $forecast->condition['data'] ?> 
    </span>  
</div> 
<?php endforeach ?> 

非常感謝您的幫助!

+0

什麼問題,你不能關閉'?>'並打開'<?php'解析器或複製粘貼? – Shef

回答

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'] ?>&deg; F – <?php echo $forecast->high['data'] ?>&deg; 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'].'&deg; F – '.$forecast->high['data'].'&deg; F,'.$forecast->condition['data']; 
      $html .= '</span>'; 
      $html .= '</div>'; 
     } 
     $html .= '<h2>The weather today ' . $information[0]->city['data'] . '</h2>' 

     return $html; 
     /* etc. etc. */ 
    } 
?> 
+0

感謝您的解決方案和改進的代碼。 – Mattvic

+0

不客氣。 – bksi

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'] .'&deg; F – '. $forecast->high['data'] .'&deg; F, 
      '. $forecast->condition['data'] .' 
     </span>  
     </div> 
    } 

然後$ html_code將所有需要的HTML代碼對return語句來串聯。

這是你所需要的?