Hi Guys
I am writing a forum and am on the admin functionality. When the admin is logged in they click on the FORUm link, and this takes them to a page, wehere I want to dislay all the different forums, then under each forum I want to display the topics for that forum. I have a topic, forum and comments table in my db.
I have tried to do the following using a nested while loop:
//this selects the topics in the topic table
$topicquery = "SELECT topic.topic_id,forum.forum_id,topic_title, topic_body FROM forum, module,topic where topic.forum_id = forum.forum_id AND module.forum_id = forum.forum_id";
$query = "SELECT * FROM forum";
$data = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($data)) {
echo '<tr align="left">';
echo '<td class="forum_id">' . $row['forum_id'] . '</td>';
echo '<td class="forum_name">' . $row['forum_name'] . '</td>';
echo '<td class="forum_description">' . $row['forum_description'] . '</td>';
echo "<td><a href='topic_create.php?forum_id=" . $row['forum_id'] ."'>Create Topic</a>";
echo '</tr></table>';
echo '<br />';
// while loop here which selects all topics associated to this forum
$topicdata = mysqli_query($dbc, $topicquery);
while ($topicrow = mysqli_fetch_array($topicdata)) {
echo '<tr align="left">';
echo '<td class="topic_id">' . $topicrow['topic_id'] . '</td>';
echo '<td class="topic_title">' . $topicrow['topic_title'] . '</td>';
echo '<td class="topic_body">' . $topicrow['topic_body'] . '</td>';
echo "<td><a href='topic_view.php?topic_id=" . $topicrow['topic_id'] ."'>View Topic</a>";
echo '</tr></table>';
echo '<br />';
} //close while
}
But when I run this, the output shows that each forum has exactly the same topics related to it, which is wrong. I think the topicquery select statement is wrong, as it selects all the topics in the db, but I cannot add a specific WHERE clause for a variable, as I want to show, all forums and all topics for each forum.
Could anyone advise me on where I am going wrong or how I could put this right to achieve what I am wanting to do. My other option is just to iterate the forum data in a while loop, then add a link to each forum called view topics, but I would like to avoid this if possible.
If anyone has any help then I would really appreciate it.
Kind regards