vendredi 7 septembre 2018

PHP: detect end of duplicate column results (calendar day)

I've been putting together a simple linear weekly event calendar script, which calls entries from a MYSQL database. It displays a sort of "8 day week" (Sunday to Sunday inclusive)

My MYSQL query is like so:

$results = $dbh->query('SELECT * FROM calendar WHERE YEARWEEK(event_date) = YEARWEEK(NOW()) OR (WEEKDAY(event_date) = 6 AND YEARWEEK(event_date) = YEARWEEK(NOW()) + 1) ORDER BY event_date ASC');

When a given day has multiple event entries, I keep the day/date heading from repeating:

$currentday = '';
$showday = true;
foreach($results as $row) {
  $weekday = date("l", strtotime($row['event_date'])); {

  if ($currentday != $row['event_date']) {
     $showday = true; 
     $currentday = $row['event_date'];
  }
  if ($showday) {
     $weekday = date("l", strtotime($row['event_date'])); 
     if($weekday == "Sunday") {
        echo "<h3 class='sunday'>";
        echo $weekday;
     }
     else {
        echo "<h3>";
        echo $weekday; 
     }
     echo date("F j", strtotime($row['event_date']));
     $showday = false;          
  }

}

... followed by the remaining details.

All that works great.

What I'm trying to accomplish now is to group each day's results visually by means of an HTML hook.

To accomplish that, I'm trying to group each day's events within a div with a class of "daybox". I can create/open the div with this, just after the first if clause:

if ($showday) { ?><div class="daybox">

That successfully opens/create a new div for each distinct day of the foreach loop.

The problem is closing it. I tried a corresponding if $showday closing code at the end of my foreach, but in retrospect, that wasn't what I was looking for, and obviously it didn't work.

What I think I need is a way to determine whether I've reached the last entry for a given day (i.e. event_date in my database), and base my if clause off that. But so far I'm stumped how I need to articulate that.

Am I on the right track, and how would I formulate my final if clause?

Thanks!

Aucun commentaire:

Enregistrer un commentaire