marky7890 Posted September 18, 2011 Share Posted September 18, 2011 Hello, I am trying to display rainfall totals for the year so far in a table with months going across and days going down. Like so: -- Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1 2 3 4 5 6 7 8 etc And then have the rainfall totals for each day in the table, I am quite experienced with PHP however I cant work out how I would do this The data is stored in a mysql table called "wx_daily", and the rainfall column is called" RainTotal". The dates are stored in the "Date" column in mysql Date format. Im using the following mysql query: SELECT * FROM wx_daily WHERE YEAR(Date) = '2011' ORDER BY Date Asc Thanks, Mark Quote Link to comment https://forums.phpfreaks.com/topic/247377-display-records-in-months-and-days-table/ Share on other sites More sharing options...
voip03 Posted September 18, 2011 Share Posted September 18, 2011 Can you tell me the table structure. Quote Link to comment https://forums.phpfreaks.com/topic/247377-display-records-in-months-and-days-table/#findComment-1270407 Share on other sites More sharing options...
marky7890 Posted September 18, 2011 Author Share Posted September 18, 2011 Here it is: `Date` date NOT NULL, `TempMin` decimal(5,1) NOT NULL, `TempMinTime` time NOT NULL, `TempMax` decimal(5,1) NOT NULL, `TempMaxTime` time NOT NULL, `DewPtMin` decimal(5,1) NOT NULL, `DewPtMinTime` time NOT NULL, `DewPtMax` decimal(5,1) NOT NULL, `DewPtMaxTime` time NOT NULL, `HumMin` decimal(5,1) NOT NULL, `HumMinTime` time NOT NULL, `HumMax` decimal(5,1) NOT NULL, `HumMaxTime` time NOT NULL, `WdChill` decimal(5,1) NOT NULL, `WdChillTime` time NOT NULL, `PressureMin` decimal(5,1) NOT NULL, `PressureMinTime` time NOT NULL, `PressureMax` decimal(5,1) NOT NULL, `PressureMaxTime` time NOT NULL, `WindGust` decimal(5,1) NOT NULL, `WindGustDir` decimal(5,1) NOT NULL, `WindGustTime` time NOT NULL, `WindSpeed` decimal(5,1) NOT NULL, `WindSpeedDir` decimal(5,1) NOT NULL, `WindSpeedTime` time NOT NULL, `Windrun` decimal(5,1) NOT NULL, `RainRate` decimal(5,1) NOT NULL, `RainRateTime` time NOT NULL, `RainTotal` decimal(5,1) NOT NULL, `SnowTotal` decimal(5,1) NOT NULL, `WindDomDir` varchar(11) NOT NULL, UNIQUE KEY `Date` (`Date`), Quote Link to comment https://forums.phpfreaks.com/topic/247377-display-records-in-months-and-days-table/#findComment-1270411 Share on other sites More sharing options...
jcbones Posted September 19, 2011 Share Posted September 19, 2011 I can't think of any way to arrange your data to do this inside the while loop that pulls the database values. But, you can store that data to sort it later. My idea is along the lines of: UN-TESTED <?php $sql = "SELECT RainTotal, MONTH(`Date`) as `month`, DAY(`Date`) as `day` FROM wx_daily WHERE YEAR(Date) = '2011' ORDER BY Date Asc"; $result = mysql_query($sql) or trigger_error(mysql_error()); if(mysql_num_rows($result) == 0) { echo 'No data exists!'; exit(); } while($r = mysql_fetch_assoc($result)) { $storage[$r['day']][$r['month']] = $r['RainTotal']; } echo '<table><tr><th>Day</th><th>Jan</th><th>Feb</th><th>Mar</th><th>April</th><th>May</th><th>June</th><th>July</th><th>Aug</th><th>Sep</th><th>Oct</th><th>Nov</th><th>Dec</th></tr>'; foreach($storage as $day => $monthArray) { echo '<tr><td>' . $day . '</td>'; for($i = 1; $i < 13; $i++) { if(array_key_exists($i,$monthArray)) { echo '<td>' . $monthArray[$i] . '</td>'; } else { echo '<td>0</td>'; } } echo '</tr>'; } echo '</table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/247377-display-records-in-months-and-days-table/#findComment-1270534 Share on other sites More sharing options...
xyph Posted September 19, 2011 Share Posted September 19, 2011 jcbones' solution is great with the following possible hiccup. It assumes you have information for every day of the month in at least one of your months. If, somehow, for some reason, you didn't have data for the 23rd day of every month in a given year, the script would skip that day. It will probably never happen like that, but I figure I'd point it out for educational sake. A solution would be to pre-populate the storage array like this: for( $i=0; $i<=31; $i++ ) $storage[$i] = array(); Quote Link to comment https://forums.phpfreaks.com/topic/247377-display-records-in-months-and-days-table/#findComment-1270540 Share on other sites More sharing options...
marky7890 Posted September 19, 2011 Author Share Posted September 19, 2011 Thanks everyone for your help, jcbones your solution worked great. Quote Link to comment https://forums.phpfreaks.com/topic/247377-display-records-in-months-and-days-table/#findComment-1270588 Share on other sites More sharing options...
jcbones Posted September 19, 2011 Share Posted September 19, 2011 jcbones' solution is great with the following possible hiccup. It assumes you have information for every day of the month in at least one of your months. If, somehow, for some reason, you didn't have data for the 23rd day of every month in a given year, the script would skip that day. It will probably never happen like that, but I figure I'd point it out for educational sake. A solution would be to pre-populate the storage array like this: for( $i=0; $i<=31; $i++ ) $storage[$i] = array(); Very true, and something that can be implemented easily. Although, I would use: $storage = array_fill(1,31,array()); Quote Link to comment https://forums.phpfreaks.com/topic/247377-display-records-in-months-and-days-table/#findComment-1270768 Share on other sites More sharing options...
xyph Posted September 19, 2011 Share Posted September 19, 2011 That would be the simpler code Never really used array_fill(), always range(). Good to know. Quote Link to comment https://forums.phpfreaks.com/topic/247377-display-records-in-months-and-days-table/#findComment-1270776 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.