cloudy

Author Topic: Need help with php foreach statement  (Read 484 times)

0 Members and 1 Guest are viewing this topic.

Offline jmar

  • Posts: 1,405
  • Halifax, Canada
    • HRM Weather
Need help with php foreach statement
« on: March 24, 2010, 01:46:34 AM »
Hi,

I am just finishing up my cell.php script that includes the forecast on the page here"
http://www.hrmweather.com/cell.php

And as can be seen, the ec forecast is returning 7 icons but the page only really accommodates 5.

This is the statement in the middle of the code that needs to be changed and I need a php guru to keep my sanity I think.

if ($fcstorg == 'EC') {
  foreach ($forecasticon as $i => $temp) {
   $forecasticons[$i] = $forecasttitles[$i] . "<br />\n" .
                        $forecasticon[$i] . "\n" .
                        $forecasttext[$i] . "\n";
  }
  $forecasttext = $forecastdetail;
  foreach ($forecastdays as $i => $temp) {
     $t = explode('<br />',$forecastdays[$i]);
     $forecasttitles[$i] = $t[0];
  }

Is there a way to modify the "foreach"  statements to limit it to the first 5 items in the arrays/ or loops?

Many thanks for the assistance,
Jim
iMac, 2 x Mac Mini, Mac OS X 10.6.7, Indigo integration, Web - WDL, Saratoga templates, Lightbox

Online saratogaWX

  • Posts: 4,577
  • Saratoga, CA, USA 37:16:28N, 122:01:23W - Elev: 374ft.
    • Saratoga Weather
Re: Need help with php foreach statement
« Reply #1 on: March 24, 2010, 06:05:01 PM »
Maybe instead of a foreach, use a for loop instead like:

  for ($i=0;$i<min(5,count($forecasticons));$i++) {

which should take the minimum of 5 or the number of forecasticons less than 5.

Best regards,
Ken
Ken True
Saratoga Weather
CWOP: CW1792
WeatherUnderground: KCASARAT1
Free weather website PHP scripts and WD website AJAX templates

Offline jmar

  • Posts: 1,405
  • Halifax, Canada
    • HRM Weather
Re: Need help with php foreach statement
« Reply #2 on: March 27, 2010, 01:28:09 PM »
Thanks Ken,
I tried that but it didn't work out for the page.

Then I read the forecast.php file's complete instructions (a la Ken True! :)  ) and followed your advice regarding setting up the page with individual variables being printed out like I do in my main website's dashboard. So I made a duplicate forecast.php page and customized it. This approach worked perfectly! Here is the test page for cell/wap on my site. Following the instructions on the top of your php forecast pages I was able to move the forecast into header'd sections and only use 5 icons across the page as desired for the phones. Still some work to do but this worked well.
http://www.hrmweather.com/cell.php
Thanks for the help!

Jim
iMac, 2 x Mac Mini, Mac OS X 10.6.7, Indigo integration, Web - WDL, Saratoga templates, Lightbox

 

cumulus