2014-09-03 50 views
0

我已經開始編寫我的論壇引擎,只是爲了刷新PHP技巧。查詢太多,php。創建精簡論壇引擎

我的問題是,當我做一個董事會和一個theard,我有雙重查詢。

我知道爲什麼,我有「foreach在不同的foreach」,但我找不到一種方法來解決它,你們可以幫助我,你會怎麼做呢?爲了避免這麼多的查詢?

舉例來說,SMF論壇主頁只有12個查詢(無數量的板子和理論)。

我的頁面已經有12個,但我只有2個類別,5個板子,4個theards,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

你爲什麼要這樣做:'sprintf(「SELECT \'id \',\'name \'FROM \'」.PREFIX。「category \'」)'? 'sprintf'在那裏做* nothing *。 – meagar 2014-09-03 03:47:45

+0

@meagar已經被刪除了,那我現在注意到了:P但是我在書中寫了不同的問題。 – WinterTime 2014-09-03 03:54:23

回答

1

而不是運行一個查詢來獲取ID列表,然後循環這些ID以獲得另一組IDS,然後遍歷這些ID以獲取一組數據,如何編寫一個查詢,使用JOIN排隊三張桌子?

+0

謝謝,你幫我:) – WinterTime 2014-09-03 11:44:50