Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:05:03+00:00 2026-06-13T10:05:03+00:00

I’m trying to list diary events in PHP from a MySQL database, but grouped

  • 0

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:

enter image description here

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:

enter image description here

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>';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:05:04+00:00Added an answer on June 13, 2026 at 10:05 am

    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
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I have a view passing on information from a database: def serve_article(request, id): served_article

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.