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
)
2
  • What is your SQL query like for this? Are the comments in a separate table, and you're using a join? Commented Oct 24, 2009 at 12:34
  • Hi Duck, yes.. I am using a total of 4 tables and the comments are in their own table. There are 3 total joins in this query. Commented Oct 24, 2009 at 12:45

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Exactly!!! This is exactly what I am looking for. Could you guide me a bit please? What I have is a database class that grabs all of my rows and returns to me an array of those values. Is there a way that I can still accomplish what you are saying the way my database method is set up?
I got it my friend. Thank you very very much!
0

you mean something like that which sorts your array first?

$sorted = array();

foreach ($unsorted AS key => $value) {
  $sorted[$value['blog_id']][] = $value;
}

(untested)

4 Comments

Hi schneck, thanks for the help. I'm not sure if sorting the data is exactly what I need or not. I just know that my database query is giving me all of the data that I need but I'm unclear as to how to show it with PHP. I will try your suggestion now. Thanks.
schneck, your code sorts everything but I am still getting 4 arrays. I suppose what I need is a multidimensional array showing the two posts on the outside and x number of comments as a seperate array within that outer array.
yes, my algorithm just sorts the original array, so that you have the correct comments assigned to their posts. The way you do the output depends on your script. please post some more of your code so that we can see how to fit my snippet in.
Hi schneck, thanks for your snippet and all the help! Anax had the solution. I suspected that I would have to build another array as he did, sorting the values first. Thank you again for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.