I am currently developing the forum as a personal project. One of the recurring issues that I encountered is database queries in loops. I have managed to avoid this so far by using table joins or caching data in arrays for later use.
Right now, although I am faced with a situation where I am not sure how I can write the code in such a way that I can easily use any of these methods. However, I would prefer to make no more than 2 requests for this operation, rather than 1 + 1 for a group of forums, which so far has led to 5 per page. Thus, although 5 is not a huge number (although it will increase for each group of the group that I add) this is a principle that is very important to me, I DO NOT want to write queries in loops
What I am doing is displaying groups of forums (e.g. administrative forums, user forums, etc.), and then each forum in this group by the index of one page, this is a combination of both pages on one page, which causes me a problem. If it were only one group per page, I would use a table join and a problem. But if I use a table join here, although I can potentially get all the data I need, it will be in one mass of results, and it should display correctly.
Here's the code (I just deleted the html part)
<?php
$sql= "select * from forum_groups";
$result1 = $database->query($sql);
while($group = mysql_fetch_assoc($result1))
{?>
<table class="threads">
<tr>
<td class="forumgroupheader"> <?php echo $group['group_name']; ?> </td>
</tr>
<tr>
<td class="forumgroupheader2"> <?php echo $group['group_desc']; ?> </td>
</tr>
</table>
<table>
<tr>
<th class="thforum"> Forum Name</th>
<th class="thforum"> Forum Decsription</th>
<th class="thforum"> Last Post </th>
<tr>
<?php
$group_id = $group['id'];
$sql = "SELECT forums.id, forums.forum_group_id, forums.forum_name, forums.forum_desc, forums.visible_rank, forums.locked, forums.lock_rank, forums.topics, forums.posts, forums.last_post, forums.last_post_id, users.username
FROM forums
LEFT JOIN users on forums.last_post_id=users.id
WHERE forum_group_id='{$group_id}'";
$result2 = $database->query($sql);
while($forum = mysql_fetch_assoc($result2))
{?>
,
a) SQL ,
)
, , .. .