I would like to group my forums into categories, as seen here:
I currently have a database table called forum_categories that takes a title and creates an ID for all the categories created. I also have a column in a database table named forum_forums (all the different forums that I want to categorize) which takes a value named category_apart_of.
How would I go about listing the forums in their correct category ID?
Thank you so much!
If you would like to see any of my code, or want me to explain anything more in-depth please tell me.
My current code for listing the forums (note: an SQL query to get all the forums is above):
<thead>
<tr>
<th style="width: 50%;">Forum</th>
<th style="width: 10%;">Threads</th>
<th style="width: 10%;">Posts</th>
<th style="width: 30%;">Latest Posts</th>
</tr>
</thead>
<tbody>
<?php
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$forumID = $row['forumID'];
$forum_title[] = $row['forum_title'];
$forum_description[] = $row['forum_description'];
$forum_total_threads[] = $row['forum_total_threads'];
$forum_total_posts[] = $row['forum_total_posts'];
$forum_latest_thread[] = $row['forum_latest_thread'];
$stmt2 = $db->prepare("SELECT * FROM forum_threads WHERE forum_thread_belongs_to = '$forumID'");
$stmt2->execute();
$count = $stmt2->rowCount();
echo '
<tr><td><h4 style="margin-bottom: 0px;"><a style="margin-bottom: 0px;" href="forum.php?id='
. $row['forumID'] . ' ">'.$row['forum_title']
. '</a></h4><br /><h6 style="margin-bottom: 0px; margin-top: 0px;">'
.$row['forum_description'].'</h6></td><td style="text-align: center;><span">'.$count
.'</span></td><td style="text-align: center;><span">'.$row['forum_total_posts']
.'</span></td><td>'.$row['forum_latest_thread'].'</td></tr>
';
}
?>
</tbody>
DDL for both tables:
CREATE TABLE IF NOT EXISTS `forum_forums` (
`forumID` int(11) NOT NULL AUTO_INCREMENT,
`forum_title` varchar(255) NOT NULL,
`forum_description` varchar(255) NOT NULL DEFAULT 'This forum does not have a description',
`forum_total_threads` int(255) NOT NULL DEFAULT '0',
`forum_total_posts` int(255) NOT NULL DEFAULT '0',
`forum_latest_thread` varchar(255) NOT NULL DEFAULT 'There are no new threads',
`forum_apart_of` int(11) NOT NULL,
`category_apart_of` int(11) NOT NULL,
PRIMARY KEY (`forumID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `forum_categories` (
`catID` int(11) NOT NULL AUTO_INCREMENT,
`cat_title` varchar(255) NOT NULL,
PRIMARY KEY (`catID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Your Question : How would I go about listing the forums in their correct category ID?
Solution : As you already have your Database Structure and as you already and should probably know in order to link your
categoriestable with yourforumstable you need to have at least one column in both similar which is acategory_idfrom yourcategoriestable auto incremented column namely asidso in order to categorize your forum into the specific category you will need to add the categoryidto an extra column ascategory_idin yourforumstable so each forum will have it's category mentioned in there in id value..!And then you can list your forums by categories like this way as :
Note : This code will check for every forum category and it will sort of list all those forums under each category..!
WORKING CODE EXAMPLE:
OUTPUT :