太多的查询,PHP。创建lite论坛引擎

时间:2014-09-03 03:44:54

标签: php mysql sql

我已经开始编写我的论坛引擎,只是为了刷新php技能。

我的问题是,当我制作一个电路板和一个电路板时,我有双重查询。

我知道为什么,我有“不同的foreach”,但我找不到解决方法,你们能帮助我,你们会怎么做?为了避免这么多的查询?

一个例子,SMF论坛主页只有12个查询(无数字板和标准)。

我的页面已经有12个,但我只有2个类别,5个板,4个理论,1个用户。

如果我添加一个电路板,那么我就有+2个查询。

<?php
if(!defined('USED'))
    exit;
# Name of page!
$page = 'Forum';

$db = new Database();
$array = $db -> select("SELECT `id`,`name` FROM `".PREFIX."category`");

if(count($array) > 0)
{
    $container .= '';
    foreach($array as $category)
    {
    $container .= '<div class="panel panel-primary" id="category-'.$category['id'].'">
    <div class="panel-heading">
            <h3 class="panel-title">
                <a href="?action#category-'.$category['id'].'">'.$category['name'].'</a>
            </h3>
    </div>
    <table class="table table-bordered table-striped table-hover">
            <thead>
            <tr>
                <th class="col-md-9">Board Name</th>
                <th class="col-md-3">Latest</th>
            </tr>
            </thead>';
    $array2 = $db -> select("SELECT `id`,`id_category`,`name`,`info` FROM `".PREFIX."boards` WHERE `id_category`=".$category['id']."");
        if(count($array2) > 0)
        {
            foreach($array2 as $board)
            {
            $container .='<tbody>
                <tr>
                        <td>
                            <a href="/forum/board/'.$board['id'].'-'.$board['name'].'"><strong>'.$board['name'].'</strong></a><br/>
                            <small>'.$board['info'].'</small>
                        </td>';
            $array3 = $db -> select("SELECT `id`,`id_board`,`title`,`created_by`,`created_date` FROM `".PREFIX."theards` WHERE `id_board`=".$board['id']." ORDER BY `created_date` DESC LIMIT 1");
                if(count($array3) > 0)
                {
                    foreach($array3 as $theards)
                    {
                        $array4 = $db -> select("SELECT `id`,`name` FROM `".PREFIX."users` WHERE `id`=".$theards['created_by']."");
                            foreach($array4 as $user)
                            {
                                $created_by = $user['name'];
                            }
                    $container .='<td>
                                <a href="/forum/theard/'.$theards['id'].'-'.$theards['title'].'">'.$theards['title'].'</a><br/><small><a href="/forum/user/'.$created_by.'">'.$created_by.'</a>, <abbr class="timeago" title="'.$theards['created_date'].'">less than a month ago</abbr></small>
                            </td>';
                    }
                }
                else
                {
                $container .= '<td>
                            Nothing is here!
                            </td>';
                }
            $container .='  </tr>
                </tbody>';
            }
        }
        else
        {
        $container .='<tbody>
                <tr>
                        <td>Nothing is here!</td>
                        <td>...</td>
                </tr>
                </tbody>';
        }
    $container .='</table>
    </div>';
    }
} 
else
{
    Site::Error("You need to add a category first!");
}

我的选择:

public function select($query) {
    $rows = array();
    $result = $this -> query($query);
    if($result === false) {
        return false;
    }
    while ($row = $result -> fetch_assoc()) {
        $rows[] = $row;
    }

    global $db_count;
    $db_count = !isset($db_count) ? 1 : $db_count + 1;

    return $rows;
}

我在文件末尾添加了$ db_count来检查查询次数。

1 个答案:

答案 0 :(得分:1)

而不是运行一个查询来获取ID列表,然后循环遍历这些ID以获取另一组IDS,然后循环这些ID以获取一组数据,如何编写一个查询,使用JOIN到行这三张桌子?