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 6809347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:04:32+00:00 2026-05-26T20:04:32+00:00

I am trying to print a recursive list where every date has a sublist

  • 0

I am trying to print a recursive list where every date has a sublist of events ordered by date.

For example on the database I have:

+------+----------+--------+
| date | event_id | post_id|
+------+----------+--------+
|date1 | event1   | post1  |
|date1 | event2   | post2  |
|date1 | event3   | post3  |
|date2 | event4   | post4  |
|date2 | event5   | post5  |
+------+----------+--------+

I need to print

<ul>
    <li>date1</li>
    <ul>
        <li>event1, post1</li>
        <li>event2, post2</li>
        <li>event3, post3</li>
    </ul>
    <li>Date 2</li>
    <ul>
        <li>event4, post4</li>
        <li>event5, post5</li>
    </ul>
</ul>

how can I print in php the

select date, event_id, post_id from tablename

query in php to have this?

  • 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-05-26T20:04:33+00:00Added an answer on May 26, 2026 at 8:04 pm

    After a while with php and mysql, I do the least I can in mysql, even if it’s often more elegant. Performance of mysql is crap. And even if what you query is fast, it can still slow down another query elsewhere done by another user. So here is a response with the least load on the DB. One query, no grouping. just select your 3 fields, and you can apply this.

    $lastDate = null;  // lastDate will be updated at each row, so we can check if it has changed the next one.
    
    echo '<ul>';
    foreach ($queryResult as $row)
    {
        if ( $lastDate != $row['date'] )
        {
            if ( $lastDate) { echo '</ul>'; }  // if lastdate "exists", it's not the first, so let't close the "date" list...
    
            echo '<li>', $row['date'], '</li><ul>'; // ... and start a new one
        }
        echo '<li>', $row['event_id'], ', ',$row['post_id'], '</li>';
        $lastDate = $row['date'];  
    
    }
    echo '</ul>'; // close the very last date list
    echo '</ul>'; // close the the full list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to print out the date in a certain format: NSDate *today =
I am trying to write a recursive function in Java that prints the numbers
I'm trying to print to Dot Matrix printers (various models) out of C#, currently
I am trying to print a message on a web page in vb.net. I
I'm trying to print the output of function only when it is true but
I am trying to print a report that contains a bar graph using the
I am trying to print invoice. But top whatever i do to setting margins,
I am trying to print a portion of the page in FF3. This works
I am trying to print 1 if there are at least two of the
I'm trying to print characters in the console at specified coordinates. Up to now

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.