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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:18:34+00:00 2026-05-12T19:18:34+00:00

Hey guys. I’m building a simple blog and have my data returning from my

  • 0

Hey guys. I’m building a simple blog and have my data returning from my database in an array. I want to show the comments associated with each blog post but I’m not sure how to organize this in a loop. This is my array:

Array
(
    [author] => Administrator
    [post] => Testing entry number one
    [entry_date] => Fri, 23 Oct 2009
    [commenter] => Sally Anderson
    [comments] => comment 1 post 1
    [comment_date] => October 24th, 2009 at 5:24 AM
    [blog_id] => 1
)
Array
(
    [author] => Administrator
    [post] => Testing entry number two
    [entry_date] => Sat, 24 Oct 2009
    [commenter] => Sally Anderson
    [comments] => comment 1 post 2
    [comment_date] => October 24th, 2009 at 5:21 AM
    [blog_id] => 2
)
Array
(
    [author] => Administrator
    [post] => Testing entry number one
    [entry_date] => Fri, 23 Oct 2009
    [commenter] => Mike
    [comments] => comment 2 post 1
    [comment_date] => October 24th, 2009 at 5:21 AM
    [blog_id] => 1
)
Array
(
    [author] => Administrator
    [post] => Testing entry number two
    [entry_date] => Sat, 24 Oct 2009
    [commenter] => Mike
    [comments] => comment 2 post 2
    [comment_date] => October 24th, 2009 at 5:21 AM
    [blog_id] => 2
)

Notice that each post repeats based on the number of comments associated with it. In this particular case this array has 2 comments associated with each post. Could someone please help me write a foreach loop that will output 2 posts with the correct comments for each one?

Thanks!


Maybe I’m not explaining well enough. Here is what I am looking for.

Array
(
    [author] => Administrator
    [post] => Testing entry number one
    [entry_date] => Fri, 23 Oct 2009
    [commenter] => Sally Anderson
    [comments] => 
        **[0] => Array
        (
           comment 1 post 1
           comment 2 post 1
        )**
    [comment_date] => October 24th, 2009 at 5:24 AM
    [blog_id] => 1
)
  • 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-12T19:18:34+00:00Added an answer on May 12, 2026 at 7:18 pm

    I would approach the subject in a different way. During your fetch results loop, I would create an array (or an object), based on blog ID. Each element of that array would contain all the common fields of the blog entry, plus an additional array to store all the comments.

    $entries = Array();
    while ($row = mysql_fetch_row($result)) {
      $key = $row['blog_id'];
      $entries[$key]['author'] = $row['author'];
      ...
      $entries[$key]['comments'][] = Array (
                         'commenter'   => $row['commenter'],
                         'comments'    => $row['comments'],
                         'comment_date'=> $row['comment_date']
                         );
    }
    

    This snippet will create the following array:

    Array
    (
        [author] => Administrator
        [post] => Testing entry number one
        [entry_date] => Fri, 23 Oct 2009
        [comments] = Array
                     (
                       Array
                       (
                         [commenter] => Sally Anderson
                         [comments] => comment 1 post 1
                         [comment_date] => October 24th, 2009 at 5:24 AM
                       )
                       (
                         [commenter] => Mike
                         [comments] => comment 2 post 1
                         [comment_date] => October 24th, 2009 at 5:21 AM
                       )
                     )
    )
    Array
    (
        [author] => Administrator
        [post] => Testing entry number two
        [entry_date] => Sat, 24 Oct 2009
        [comments] = Array
                     (
                       Array
                       (
                         [commenter] => Sally Anderson
                         [comments] => comment 1 post 2
                         [comment_date] => October 24th, 2009 at 5:21 AM
                       )
                       (
                         [commenter] => Mike
                         [comments] => comment 2 post 2
                         [comment_date] => October 24th, 2009 at 5:21 AM
                       )
                     )
    )
    

    Now during your presentation loop, you iterate through your array to get the blog posts (so functions such as ‘count’ will also give you the number of blog entries) and when you display each entry you just iterate through the comments element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey guys having this really simple problem but cant seem to figure out have
hey guys i wanna ask a question about sending data to database to server
Hey guys, here's my requirement: I have one central App which receives requests from
Hey guys im trying to get a simple button masher up, What i want
hey guys, I have mypage.com/user-login.php?action=register and I want to be able to call mypage.com/register
hey guys, i have a usercontrol within the default email compose form, I want
hey guys, i have a string which contains say 100 words, now i want
Hey guys I have a object sitting in my tile map for spawn point
Hey guys am having a bit of problems with my jQuery, I have all
Hey guys got an issue with Cakephp validation.. I want to know why is

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.