Reputation: 65
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'] . '&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!
Upvotes: 1
Views: 299
Reputation: 28369
I suspect what you're going to have to do is redesign your database to store the thread/subject relationship in a field so that you can pull all the threads in one query and then sort them programmatically in php. If you have a parent_id field and each reply row knows who its parent is and what its order is (presumably the order is simply id > previous id) it's pretty simple to then organize them as you need them.
You can then walk your response list and throw all the items into JSON(y) Object that has all your nested responses in their correct order/relationship.
To reduce unwanted bloat, you may want to store the top level parent ID as well, so that you can easily pull only those threads that belong to specific top level threads. That way you can easily grab only the threads that belong to, for instance, the last 20 top level threads (so as to reduce unnecessary computing on ancient threads that no one needs, and to build some sort of pagification).
EDIT... some pseudo code...
select * from row order by top_level_id desc limit 0,20
// this will give you a max of 20 top level threads, so you're not pulling the entire db. The 0 at the end should be a page variable you pass in from your PHP, so you can do 20,20 for page 2 and 40,20 for page 3
now you do something like this (obviously this is pseudo code, but you should be able to get it done based on this model...
$sortedRows; //this will be a JSON object, or hash array
for($row in result){
addToThreadObj($row);
}
function addToThreadObj($row){
if( containsParent($row.parent_id))
addChildToParent($row.parent_id, $row);
else
createNewParent($row);
}
Upvotes: 2