0

Hi everybody,

I'm wondering where I went wrong. The idea is I'm trying to get data from the database and display in this format

Question 1:
Answer:
Answer:
Answer:
Answer:

Question 2:
Answer:

Question 3:
No answers
etc..

foreach($posts as $post){
          $id = (int)$post['id'];
          $comments = get_comments($id);
        //getting the answers
        foreach($comments as $comment){
        echo "<h1>" .$post['title'] ."  id:" .$post['id'] ."</h1>";
        echo $post['body'] ."<br/>";
        echo "  <small>Asked at " .$post['date_published'] ."</small>";
        echo "<br/>";
        echo "<br/>";
        echo "<strong> <div>";  
        echo  $comment['body'] ."<br/>";
        echo "Answered by " .$comment['answered_by'] ."</br>";
        echo "<small>" .$comment['date_published'] ."</small>";
        echo "</div> </strong>";

      }
}

Thank you so much in advance. It would be good to give a brief explanation with examples of how Nested foreach loops work.

Edited by rotten69

2
Contributors
7
Replies
9
Views
4 Years
Discussion Span
Last Post by rotten69
1

Something like this?

// loop through all posts
foreach ($posts as $post) {

    // get the comments for this post
    $id = (int)$post['id'];
    $comments = get_comments($id);

    // output the post information here
    echo "<h1>" .$post['title'] ."  id:" .$post['id'] ."</h1>";
    echo $post['body'] ."<br/>";
    echo "  <small>Asked at " .$post['date_published'] ."</small>";
    echo "<br/>";
    echo "<br/>";

    // start comments part
    echo "<strong> <div>";  

    // check if no comments
    if (count($comments) == 0) {
        echo "No comments";
    }

    // output all comments
    foreach ($comments as $comment){
        echo  $comment['body'] ."<br/>";
        echo "Answered by " .$comment['answered_by'] ."</br>";
        echo "<small>" .$comment['date_published'] ."</small>";
    }

    // end comments part
    echo "</div> </strong>";
}
Votes + Comments
thanks
0

Will that display the comments for a particular question in this format?

--> Question1

  • Comment One
  • Comment Two
  • CommentThree
  • Etc..

I will definitely try it. That'd be helpful if you were to explain nested foreach loops. I want to understand how it works. Try to use simple arrays of names or numbers just to avoid confusion.

0

That'd be helpful if you were to explain nested foreach loops. I want to understand how it works. Try to use simple arrays of names or numbers just to avoid confusion.

I am not convinced such an example would be simpler as the one above. Do you have anything particular in mind?

0

I want to understand how it actually works. Does the outter foreach get executed first so after it is done, the inner gets executed?

0

Ah. No. The outer one is leading. Meaning it gets executed first. But when the code is executed and the second is encountered, that one is executed until it's done. After that the first loop ends, and if there is another loop in the first, that one will be executed again (hence the nesting). For example:

<?php
    $data = array (
        1 => array (
            'title' => 'Using PHP',
            'comments' => array (
                'Nice article!',
                'Thanks for explaining!'
            )
        ),
        2 => array (
            'title' => 'Using loops',
            'comments' => array (
                'This could be better.',
                'Did you write this in 5 minutes?',
                'Please show more. Kinda vague'
            )
        )
    );

    foreach ($data as $post) {
        // This loops items 1 and 2
        echo "<h1>{$post['title']}</h1>";

        foreach ($post['comments'] as $comment) {
            // this loops the comments for the current item
            echo "<p>$comment</p>";
        }
    }
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.