Jump to content

Display records in Months and Days table


marky7890
 Share

Recommended Posts

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  :shrug:

 

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

Link to comment
Share on other sites

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`),

Link to comment
Share on other sites

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>';

?>

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.