Note that there are some explanatory texts on larger screens.

plurals
  1. POMoving foreach loop before return statement
    primarykey
    data
    text
    <p>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. </p> <p>I´m trying to create a function like this: :</p> <pre><code>function getWeatherForecast($atts) { all variables go here; return 'concatenated output'; } </code></pre> <p>Here a link to the full script: <a href="http://pastebin.com/fcQpskmy" rel="nofollow">http://pastebin.com/fcQpskmy</a></p> <p>This is what I have now: </p> <pre><code>function getWeatherForecast($atts) { $xml = simplexml_load_file('http://www.google.com/ig/api?weather=barcelona'); $information = $xml-&gt;xpath("/xml_api_reply/weather/forecast_information"); $current = $xml-&gt;xpath("/xml_api_reply/weather/current_conditions"); $forecast_list = $xml-&gt;xpath("/xml_api_reply/weather/forecast_conditions"); /* The foreach loop should go here */ return '&lt;h2&gt;The weather today ' . $information[0]-&gt;city['data'] . '&lt;/h2&gt;'; /* etc. etc. */ } </code></pre> <p>And this is the foreach loop that I need to place before the return statement: </p> <pre><code>&lt;?php foreach ($forecast_list as $forecast) : ?&gt; &lt;div&gt; &lt;img src="&lt;?php echo 'http://www.google.com' . $forecast-&gt;icon['data']?&gt;" alt="weather"?&gt; &lt;div&gt;&lt;?php echo $forecast-&gt;day_of_week['data']; ?&gt;&lt;/div&gt; &lt;span&gt; &lt;?php echo $forecast-&gt;low['data'] ?&gt;&amp;deg; F – &lt;?php echo $forecast-&gt;high['data'] ?&gt;&amp;deg; F, &lt;?php echo $forecast-&gt;condition['data'] ?&gt; &lt;/span&gt; &lt;/div&gt; &lt;?php endforeach ?&gt; </code></pre> <p>Thanks so much for your help!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload