How to remove this request from a loop?

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"; //query 1
    $result1 = $database->query($sql);
    while($group = mysql_fetch_assoc($result1)) //first loop
      {?>
        <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}'";
                //query 2
                $result2 = $database->query($sql);
                while($forum = mysql_fetch_assoc($result2))
                                            //second loop        
                    {?>

,  a) SQL ,  )
, , .. .

+3
2

, , . , ( ) . , , . , group_id. , .

, , .

, :

$currentGroupId = 0;
$firstTime = true;
while ($row = mysql_query_fetch_assoc($res)) {
    if ($row['group_id'] != $currentGroupId) {
        $currentGroupId = $row['group_id'];
        if (!$firstTime) {
            // echo close group html
        } else {
            $firstTime = false;
        }
        // echo open group html
    }

    // do forum stuff
}
// echo close group html
+2

. ( SELECT *). forum_groups , :

SELECT forum_groups.id, ...other columns here...
FROM forum_groups 
JOIN forums ON forum_groups.id = forum_group_id
LEFT JOIN users ON forums.last_post_id = users.id

, , , , , .

+3

Source: https://habr.com/ru/post/1739642/


All Articles