While loops within while loops and output php?

Posted by NovacTownCode on Stack Overflow See other posts from Stack Overflow or by NovacTownCode
Published on 2011-11-20T17:39:23Z Indexed on 2011/11/20 17:50 UTC
Read the original article Hit count: 538

Filed under:
|
|
|

I have a while loop to show the replies for a post on my website.

The value for parentID used in the query is $post['postID'] which is an array of details for the post being viewed.

As seen below it outputs the following (each subject is a link to view the full post)

$q = $dbc -> prepare("SELECT * FROM boardposts WHERE parentID = ?");
$q -> execute(array($post['postID']));
while ($postReply = $q -> fetch(PDO::FETCH_ASSOC)) {
     echo '<p><a href="http://www.example.com/boards?topic=' . $_GET['topic'] . '&amp;view=' . $postReply['postID'] . '">' . $postReply['subject'] . '</a>';
}

This currently outputs something along the lines of,

Replies To This Message:

subject 1
subject 2
subject 3
subject 4

Is there a way in which I can also in the list include replies to the replies, something along the lines of,

Replies To This Message:

subject 1
         subject 1 reply
         subject 1 reply
                 subject 1 reply reply
subject 2
subject 3
         subject 3 reply
         subject 3 reply
                 subject 3 reply reply
subject 4
         subject 4 reply
subject 5
subject 6
         subject 6 reply
                 subject 4 reply reply

I understand all the indenting can be with css, but am stuck as to how to pull the data from the mysql database and in the correct order, I tried while loops within while loops, but that involved queries inside while loops, which is bad!

Thanks for your input!

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql