简体   繁体   中英

Listing diary events grouped by days in PHP/MySQL

I'm trying to list diary events in PHP from a MySQL database, but grouped by days. I'll explain.

This is a screenshot of what I have so far:

在此输入图像描述

As you can see there are two diary events for the 23rd October 2012 and is showing two calendar icons/representations/whatever's. I actually want it to show one calendar icon on the left but list all of that days events on the right, until the next day - as seen in my a̶r̶t̶i̶s̶t̶s̶ idiots impression below:

在此输入图像描述

This is the code I have just written, could somebody please point me in the right direction:

$SQL = "SELECT entry_id, entry_title, entry_body, entry_date, entry_day, entry_month, entry_year ";
$SQL .= "FROM pages_diary WHERE entry_month = :this_month AND entry_year = :this_year ";
$SQL .= "ORDER BY entry_date DESC;";
// PDO stuff
if ($STH->rowCount() > 0) {
    while($row = $STH->fetch()):
        $this_db_month_word = mktime(0, 0, 0, $row['entry_month']);
        $this_db_month_word = strftime("%b", $this_db_month_word);
        echo '<div class="diary_events_item" id="diary_events_item_'.$row['entry_id'].'">';
            echo '<div class="diary_events_item_left">';
                echo '<div class="calendar_wrap">';
                    echo '<div class="calendar_wrap_top">';
                        echo $this_db_month_word;
                    echo '</div>';
                    echo '<div class="calendar_wrap_bottom">';
                        echo str_pad($row['entry_day'],2,'0',STR_PAD_LEFT);;
                    echo '</div>';
                echo '</div>';
            echo '</div>';
            echo '<div class="diary_events_item_right">';
                echo '<strong>'.htmlspecialchars($row['entry_title']).'</strong><br>';
                echo '<p>'.htmlspecialchars($row['entry_body']).'</p>';
            echo '</div>';
            echo '<div class="clear"></div>';
        echo '</div>';
    endwhile;
} else {
    echo '<p>There are no diary entries logged for <strong>'.$this_month_word.' '.$this_year.'</strong>.</p>';  
}

Not looking for exact code (although that would be spiffing), just an explanation would do, I'm sure I can work it out from that.

THE FIX

At the end of the WHILE loop, I added:

$last_db_day = $row['entry_day'];

And then wrapped the calendar item in:

if ($last_db_day != $row['entry_day']) {
    echo '<div class="calendar_wrap_top">';
        echo $this_db_month_word;
    echo '</div>';
    echo '<div class="calendar_wrap_bottom">';
        echo str_pad($row['entry_day'],2,'0',STR_PAD_LEFT);;
    echo '</div>';
}

As you loop over your resultset from the database, keep track of the last entry_date that you've seen and only output a new calendar icon if the current record is for a different date.

I usually do this as follows:

if ($STH->execute()) {

  // output headers

  $row = $STH->fetch();
  while ($row) {
    $current_date = $row['entry_date'];

    // output $current_date initialisation
    do {
      // output event $row
    } while ($row = $STH->fetch() and $row['entry_date'] == $current_date);
    // output $current_date termination
  }

  // output footers

}

Although I would go the other way around and embed the PHP within the HTML rather then the other way around, here is what I would do using your code:

$SQL = "SELECT entry_id, entry_title, entry_body, entry_date, entry_day, entry_month, entry_year ";
$SQL .= "FROM pages_diary WHERE entry_month = :this_month AND entry_year = :this_year ";
$SQL .= "ORDER BY entry_date DESC;";
// PDO stuff
if ($STH->rowCount() > 0) {
    $loopDay = '';

    while($row = $STH->fetch()):

        if ($row['entry_day'] != $loopDay) {
            $loopDay = $row['entry_day'];
            $changed = true;
        }


        $this_db_month_word = mktime(0, 0, 0, $row['entry_month']);
        $this_db_month_word = strftime("%b", $this_db_month_word);
        echo '<div class="diary_events_item" id="diary_events_item_'.$row['entry_id'].'">';
            echo '<div class="diary_events_item_left">';

                if ($changed) {
                    echo '<div class="calendar_wrap">';
                        echo '<div class="calendar_wrap_top">';
                            echo $this_db_month_word;
                        echo '</div>';
                        echo '<div class="calendar_wrap_bottom">';
                            echo str_pad($row['entry_day'],2,'0',STR_PAD_LEFT);;
                        echo '</div>';
                    echo '</div>';
                } else {
                    echo '&nbsp;';
                }

            echo '</div>';
            echo '<div class="diary_events_item_right">';
                echo '<strong>'.htmlspecialchars($row['entry_title']).'</strong><br>';
                echo '<p>'.htmlspecialchars($row['entry_body']).'</p>';
            echo '</div>';
            echo '<div class="clear"></div>';
        echo '</div>';

    $changed = false;
    endwhile;
} else {
    echo '<p>There are no diary entries logged for <strong>'.$this_month_word.' '.$this_year.'</strong>.</p>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM