Welcome Guest, Not a member yet? Register   Sign In
How to loop though a list of blog entries and all their comments on the same page
#1

[eluser]Unknown[/eluser]
Hi folks!

I have a simple database structure like this:

Code:
entries table
+----+-----------+------------+-----------+------+
| id | date_time | post_title | post_slug | body |
+----+-----------+------------+-----------+------+

comments table
+----+-----------+----------+------+--------+
| id | date_time | entry_id | body | author |
+----+-----------+----------+------+--------+

I am in a situation where I need to loop through all my etnries and all the comments on the same page. How do I go on and do this in the best possible way (performance and maintainability)

Do I need to first loop through all my entries and then within that loop get all it's comments and loop through them? That means alot of queries to the database. Is there a better way?

The desired output looks like this:
Code:
Entry #1
  Comment #1
  Comment #2
Entry #2
  Comment #1
  Comment #2
  Comment #3
Entry #3
Entry #4
  Comment #1

I hope this is not a duplicate of other posts but I searched for a long time and couldn't find anything...

Thanks!
#2

[eluser]srpurdy[/eluser]
[quote author="Fred Bergman" date="1290870486"]Hi folks!

I have a simple database structure like this:

Code:
entries table
+----+-----------+------------+-----------+------+
| id | date_time | post_title | post_slug | body |
+----+-----------+------------+-----------+------+

comments table
+----+-----------+----------+------+--------+
| id | date_time | entry_id | body | author |
+----+-----------+----------+------+--------+

I am in a situation where I need to loop through all my etnries and all the comments on the same page. How do I go on and do this in the best possible way (performance and maintainability)

Do I need to first loop through all my entries and then within that loop get all it's comments and loop through them? That means alot of queries to the database. Is there a better way?

The desired output looks like this:
Code:
Entry #1
  Comment #1
  Comment #2
Entry #2
  Comment #1
  Comment #2
  Comment #3
Entry #3
Entry #4
  Comment #1

I hope this is not a duplicate of other posts but I searched for a long time and couldn't find anything...

Thanks![/quote]

This is a little better than what your suggesting above, but not much, using pagination though should help a bit with my below suggestion.

You might be able to do it by creating a complex query, where you grab everything. So using a join to join the comments table with the blog table. Than have 1 other query for a count, to count how many comments are in the specific entry.

Than do a for loop over the html output that you want to display for the comments with a count on the for loop so you return the correct amount of comment results per entry.

So instead of having a query per comment you'd have a query per blog post. Not quite as bad. Smile

Shawn




Theme © iAndrew 2016 - Forum software by © MyBB