Simple forum?

I’m trying to create a simple forum,
If I go to a categories page, http://coronadoshores.ca/community/category.php?id=1&topic=Welcome
I get a link to see the full post as well as the # of replies and as link to them, http://coronadoshores.ca/community/post.php?id=3&topic=Welcome
Is there a way to see the replies (not just the #) on the category.php page?

Of course. Can’t you just use the same code inside post.php?

Tried it and failed… Heres the query inside post.php

SELECT mess.subject, mess.message_txt, mess.mess_id, mess.parent_id, mess.created, users.name
FROM messages AS mess
INNER JOIN `users` 
ON users.id =  mess.user_id
WHERE mess.parent_id = :id
OR mess.mess_id = :id
ORDER BY mess.created

but the id is attached to the URL, http://coronadoshores.ca/community/post.php?id=3&topic=Welcome

whereaws the query for the category.php page is

SELECT mess.subject, mess.message_txt, mess.mess_id, mess.parent_id, mess.created, users.name,
COUNT(rply.parent_id) AS replies
FROM messages AS mess
INNER JOIN users 
ON users.id =  mess.user_id
LEFT OUTER JOIN messages AS rply
ON rply.parent_id = mess.mess_id
WHERE mess.topic_id = :id
AND mess.parent_id IS NULL
GROUP BY mess.mess_id

but the id also is attached to the URL, http://coronadoshores.ca/community/category.php?id=1&topic=Welcome

Since im counting em, I should also be able to select em…and display them in a loop…

You are doing a loop in the category page with this code:

You can use this code in the loop, right?

Where you need to change the ‘mess.mess_id = :id’ to be the ‘id’ you use in the loop.

Did I explain it correct?

Im sorry but I am sort of confused… Are you saying I run 2 queries at the page or that I need to modify the query on the category.php page?

Exactly, 2 queries.
1 that you already have it in category.php and the other one, from post.php inside the loop you use in category.php
Is it more clear now?

ok, I see what you mean, so is this how I would run that query (loop it to print all the results) within the other query (that produces 1 result)

try {

$stmt = $dbh->prepare('SELECT mess.subject, mess.message_txt, mess.mess_id, mess.parent_id, mess.created, users.name,
COUNT(rply.parent_id) AS replies
FROM messages AS mess
INNER JOIN users 
ON users.id =  mess.user_id
LEFT OUTER JOIN messages AS rply
ON rply.parent_id = mess.mess_id
WHERE mess.topic_id = :id
AND mess.parent_id IS NULL
GROUP BY mess.mess_id');

$stmt->execute(array(
':id' => $id 
));	
while($row = $stmt->fetch()) {
	
$prev = substr($row['message_txt'],0,85);

echo "<div class='panel panel-default'><div class='panel-heading'>";
echo "<span class='pull-right'>";
echo "<small>Post by ".$row['name'];
echo " on ".date( 'm/d/y g:i A', strtotime($row['created']))."</small>";
echo "<a href='post.php?id={$row['mess_id']}&topic=".$_GET['topic']."'>";
echo "<span class='glyphicon glyphicon-share-alt' style='margin:0 5px 0 15px'></span>";
echo "replies <span class='badge'>{$row['replies']}</span></a>";
echo "</span>";
echo "<a data-toggle='collapse' data-parent='#accordion' href='#collapse{$row['mess_id']}' aria-expanded='true' aria-controls='collapse{$row['mess_id']}'>";
echo "<h3 class='panel-title' style='margin-right:175px; font-size:150%'>".$row['subject']."</h3>";
echo "</a>";
echo "</div>\n";
echo "<div id='collapse{$row['mess_id']}' class='panel-collapse collapse' role='tabpanel' aria-labelledby='heading{$row['mess_id']}'>";
echo "<div class='panel-body comment more'>".$row['message_txt'];
echo "</div>";
try {

$stmt = $dbh->prepare('SELECT mess.subject, mess.message_txt, mess.mess_id, mess.parent_id, mess.created, users.name
FROM messages AS mess
INNER JOIN `users` 
ON users.id =  mess.user_id
WHERE mess.parent_id = :par_id
ORDER BY mess.created');

$stmt->execute(array(
':par_id' => $row['mess_id]
));	

while($row = $stmt->fetch()) {
echo "<div class='well well-sm'>".$row['message_txt'];
echo "<p class='text-right small'>".$row['name']." on ".date( 'm/d/y g:i A', strtotime($row['created']))."</p>";
echo "</div>\n";
}
}
echo "</div>";
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
} 
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}

Is that right?

You should tell us, is that working?

If isn’t working upload the two files in any uploading code site.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.