I'm currently "practicing" making a forum. I've never done anything this big before and it's been going on for a while. Pretty much everything works, but I can't get it to select the proper latest topic from the categories on the index page.
Here is the SQL:
$sql = '
SELECT topic_id, topic_subject, topic_by FROM topics WHERE topic_cat IN (1,2,3) ORDER BY topic_cat DESC LIMIT 3
UNION
SELECT cat_id, cat_name, cat_description FROM categories WHERE cat_id IN (1,2,3)
';
I will also supply some pictures of the two tables from the database.
Here is the categories table.
This is the topics table.
The basic idea here, as you may tell from the SQL, is that it selects the three first categories and then selects the latest topic from those three categories.
The code for placing the information retrieved by sql into a table containing everything.
$result = mysqli_query($conn, $sql);
if (!$result) {
echo 'Could not display categories. Error: ' . mysqli_error($conn);
} else {
if (mysqli_num_rows($result) == 0) {
echo 'No categories found in the database.';
} else {
echo '
<table>
<h3>Top 3 Categories</h3>
<tr>
<th>Category</th>
<th>Latest Topic</th>
</tr>
';
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>
<td class="leftpart">
<h3><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'] . '
</td>
<td class="rightpart">
<a href="topic.php?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a>
</td>
</tr>
';
}
echo '</table>';
}
}
I'm not sure what else to add to this, so if there are any questions, please comment it and I can either answer it there or add additional information to the main post!