Hi there,
I have created a page that executes 3 very similar queries in order to retrieve the threads (in a discussion board) that belong to a certain topic.
In my database, I have a column called threadTopic that specifies which topic the thread in the table belongs to. So when I for example, SELECT * FROM threads WHERE threadTopic=generalDiscussion, it will only retrieve the threads related to the generalDiscussion topic; which can then be displayed in their own seperate table.
However, I have to do this process every time for every threadTopic option (there will be 5 altogether).
My question isnt looking for correction on any code I have done as it works fine, but more a question of best practice as the way I have done it feels messy, inefficient, time-consuming and cheap.
Is there a technique where I can use a foreach to get each threadTopic and then display all the threads in the Topic so that I will end with 5 tables from one query?
This is the query I am repeating, with a different SQL query to match each threadTopic:
$query = 'SELECT * FROM threads WHERE threadTopic="general"';
$output = $db->query($query);
$results = $output->fetchAll(PDO::FETCH_ASSOC);
if (count($results) > 0) {
echo "<table>\n";
echo "<tr>
<th>General Discussion</th>
<th>Replies</th>
<th>Last activity</th></tr>\n";
foreach($results as $result){
$rand = rand(000, 999);
echo "<tr>\n";
echo "<td>$result[threadName]</td>";
echo "<td>$rand</td>";
echo "<td >$result[lastActive]<br>By: <a href=\"#\">$result[threadBy]</a></td>";
}
echo "</tr>\n";
echo "</table>";
}