Print Table of results for every day of the year
I have an a开发者_C百科rray that looks like this.
Array (
[1] => Array (
[1] => Array ([0] => 12)
[2] => Array ([0] => 23)
# up to 12.
# up to 31.
It runs from array 1 to 31 (for each possible day of the month), then inside that array 1-12 (which is each month).
I need to create a HTML table which looks like:
Date January February and so on... to December
2011-01-01 12 23
So, dates on the first column and top row is the months.
What's the best way I can create this table? Help is much appreciated.
The best way is to convert the array into a new array that contains all the dates (rows) and per each row it contains another array that consists of all the months (columns).
This format is much easier for display.
Exmaple:
Array (
[1] => Array (
[1] => Array ([0] => 12)
[2] => Array ([0] => 23)
# up to 12.
# up to 31.
Ups, no need to convert it! Let's give that array a name: $dates
:
echo '<pre>';
foreach($dates as $rowIndex => $row) {
printf('Row header (%2d):', $rowIndex);
foreach($row as $col) {
printf('%-10s', $col);
}
printf("<br>\n")
}
echo '</pre>';
I leave adding the headings on top up to you.
Assuming that your array is called $data
:
<table>
<thead>
<th>Date</th>
<th>January</th>
<th>February</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>August</th>
<th>September</th>
<th>October</th>
<th>November</th>
<th>December</th>
</thead>
<tbody>
<?php foreach ( $data as $key => $value ): ?>
<tr>
<th><?php echo date('Y-m-d', mktime(0, 0, 0, (int)date('m'), (int)$key)); ?></th>
<?php for ( $i = 1; $i <= 12; $i++ ): ?>
<td><?php echo ! empty($value[$i][0]) ? $value[$i][0] : ' '; ?></td>
<?php endfor; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
精彩评论