开发者

Moving foreach loop before return statement

开发者 https://www.devze.com 2023-04-07 12:42 出处:网络
I´m trying to place the result of a weather forecast function in a PHP return statement. To do this using concatenation I need to move a foreach loop up in the code and I just can´t get it to work.

I´m trying to place the result of a weather forecast function in a PHP return statement. To do this using concatenation I need to move a foreach loop up in the code and I just can´t get it to work.

I´m trying to create a function like this: :

function getWeatherForecast($atts) {
    all variables go here;
    return 'concatenated output';
}

Here a link to the full script: http://pastebin.com/fcQpskmy

This is what I have now:

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. */
}

And this is the foreach loop that I need to place be开发者_运维百科fore the return statement:

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

Thanks so much for your help!!


i tried this and it works:

<?
    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. */
    }
?>

but this is not right way to do that. Use one variable to accumulate html, and then return all the html. like this:

<?
    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. */
    }
?>


Just so we're clear, you want that foreach to be inside the function right?

In that case:

 $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>
  }

And then $html_code will have all the html code needed to be concatenated on the return statement.

Is this what you need?

0

精彩评论

暂无评论...
验证码 换一张
取 消