Good afternoon everyone.
I've got a MySQL table organized like this:
| ID | Date | Some_Information |
+----+------------+----------------------------+
| 1 | 2016-03-02 | A note about this day... |
| 2 | 2016-03-22 | A note about that day... |
| 3 | 2016-04-05 | Another note... |
I need to display this data in a similiar way to Google Calendar app's agenda view. To do this I should:
<div>
with some heading for the corresponding month.Can someone please tell me if this is possible, and how?
I made some research. I solved this in a simple way.
I used this query to fetch result for the last month, mixing it with an incremental variable, named $offset
, and putting it in a for cycle:
$offset = 1;
$sub_offset = $offset - 1;
$query = "SELECT * FROM table WHERE (date >= last_day(now()) + interval 1 day - interval ".$offset." month and date <= last_day(now()) + interval 1 day - interval ".$sub_offset." month) ORDER BY date DESC";
At the beginning of the for
cycle, I declare once $offset
, echo the month heading, execute the query and display its results via while
and mysqli_fetch_assoc()
. Then I increment the $offset
variable, and the for
cycle repeats itself, just until I want it to display months.
MySQL:
SELECT
table.ID,
table.date,
table.Some_Information,
MONTH(table.date) AS m,
YEAR(table.date) AS y
FROM table
ORDER BY table.date DESC
PHP:
$lastYm = '';
foreach($result as $row) {
$ym = $row['y'] . '-' . $row['m'];
if($ym != $lastYm) {
echo '<div class="month">' . $row['m'] . '</div>';
$lastYm = $ym;
}
echo '<div class="info">' . $row['Some_Information'] . '</div>';
}