2012-08-06 82 views
-1

我不知道爲什麼,但我的腳本的子類別「論壇」沒有被搜索到相應的類別。相反,子類別強制腳本重複類別代碼。PHP + MySQL類別不應顯示爲

如下圖所示,有一個類別Test,該類別爲theres Test and Bugs。然而,Bugs進入第二個表格,它不應該是兩個測試類別,而是它看起來像數據庫中有兩個名爲Test的類別。我的代碼中有什麼錯誤,Bugs被推入副本 - 類別「測試」而不是正確的測試 - 只是一個簡單的測試論壇?

enter image description here

這裏是代碼。

<?php 

include 'connect.php'; 
include 'header.php'; 

$sql = "SELECT 
      categories.cat_id, 
      categories.cat_name, 
      forums.forum_id, 
      forums.forum_cat, 
      forums.forum_name, 
      forums.forum_desc, 
      COUNT(forums.forum_id) AS forums 
     FROM 
      categories 
     LEFT JOIN 
      forums 
     ON 
      forums.forum_cat = categories.cat_id 
     GROUP BY 
      forums.forum_name, forums.forum_desc, forums.forum_id 
     ORDER BY 
      categories.cat_id ASC 
     "; 

$result = mysql_query($sql); 

if(!$result) 
{ 
    echo 'The categories could not be displayed, please try again later.'; 
} 
else 
{ 
    if(mysql_num_rows($result) == 0) 
    { 
     echo 'No categories defined yet.'; 
    } 
    else 
    { 
     //prepare the table   
     while($row = mysql_fetch_assoc($result)) 
     { 

     echo '<table border="1"> 
       <tr> 
       <th>' . $row['cat_name'] . '</th><th></th> 
       </tr>';  
      echo '<tr>'; 

       echo '<td class="leftpart">'; 
        echo '<h3><a href="viewforum.php?id=' . $row['forum_id'] . '">' . $row['forum_name'] . '</a></h3>' . $row['forum_desc']; 
       echo '</td>'; 
       echo '<td class="rightpart">'; 

       //fetch last topic for each cat 
        $topicsql = "SELECT 
            topic_id, 
            topic_subject, 
            topic_date, 
            topic_cat 
           FROM 
            topics 
           WHERE 
            topic_cat = " . $row['forum_id'] . " 
           ORDER BY 
            topic_date 
           DESC 
           LIMIT 
            1"; 

        $topicsresult = mysql_query($topicsql); 

        if(!$topicsresult) 
        { 
         echo 'Last topic could not be displayed.'; 
        } 
        else 
        { 
         if(mysql_num_rows($topicsresult) == 0) 
         { 
          echo 'no topics'; 
         } 
         else 
         { 
          while($topicrow = mysql_fetch_array($topicsresult)) 
          echo '<a href="viewtopic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date'])); 
         } 
        } 
       echo '</td>'; 
      echo '</tr>'; 
      echo '</br>'; 
     } 
    } 
} 

include 'footer.php'; 
?> 

表結構

分類

cat_id  | int(8) | primary | auto incr 
cat_name | var(255) 

|2|Damm 
|3|Hmm 
|1|Test 

論壇

forum_id | int(8) | primary | auto_incr 
forum_cat | int(8) <-- forum cat "category" is just ID of category it belongs to 
forum_name | var(255) 
forum_desc | var(255) 

|1|1|Test|Just a simple forum test 
|2|3|Lol 
|3|1|Bugs|Bugs and related go here 

主題

topic_id | int(8) | primary | auto_incr 
topic_subject | var(255) 
topic_date | datetime 
topic_cat  | int(8) 
topic_by  | int(8) 

|1|Test|2012-07-31 23:12:47|1|1 

我個人嘗試過不同的事情,但仍然無法弄清楚爲什麼會發生這種情況。

+0

你可以發佈相關表格的結構嗎? – David 2012-08-06 00:16:54

+0

我並不十分關注這個問題。我建議添加非規範化的類別表中的「最後一個主題」(保存多個查詢,而不是關鍵更新兩個項目兩次),但不能遵循這個問題。 – Robbie 2012-08-06 00:49:15

+0

@Robbie我不想聽起來像一個屁股洞,但你爲什麼認爲我把圖像?正如Bugs所說的那樣,它正在製作一個它不應該的副本 - 類別「測試」,相反它必須在測試下正確 - 只是一個簡單的測試論壇,但正如你在圖片中看到的那樣,它載入一個副本 - 類別而不是實際一個它必須在 – 2012-08-06 03:11:51

回答

1

您將有沿線的結果

cat_id 1, forum_id 1 
cat_id 1, forum_id 2 
cat_id 1, forum_id 3 
cat_id 2, forum_id 4 

但你在一個環路輸出的一切,所以你需要做的就是記住最後一次顯示的標題,只顯示一個新的標題,如果你在做什麼在一個新的類別。

下面是一個例子,並不完美,但會給你一個開始

// Start the table here - outside the loop 
echo '<table border="1"> 

// Here's how you track if you need the header 
$lastCatID = -1 

// Now loop 
while($row = mysql_fetch_assoc($result)) 
{  
    // Only show cat header on condition 
    if ($lastCatID <> $row['cat_id']) { 
     echo ' 
      <tr> 
      <th>' . $row['cat_name'] . '</th><th></th> 
      </tr>'; 

     // Note that you've shows this header so you don't show it again. 
     $lastCatID = $row['cat_id']; 
    } 

    // Now output the rest  
    echo '<tr>'; 
    echo '<td class="leftpart">'; 
     etc.... 
    echo '</tr>'; 
} 

// Now close the table now you're all done looping. 
echo '</table>'; 

希望有所幫助。你應該能夠從那裏擴展到適合你的風格。