我面臨一些問題,我真的不知道我可能會做錯什麼。我想要做的是創建一個專有論壇,我可以爲我的客戶和我自己的項目使用。我同意這簡單的愚蠢試圖重新發明輪子,而不是僅僅撕開phpBB,但這也是我學習錯誤的好方法。無法根據SQL輸出創建單獨的論壇類別
問題 問題在於PHP腳本循環的類別。例如,我們在SQL中有3個類別。餅乾,蛋糕和咖啡現在我們有2個論壇每個說喜歡,並在適當的論壇不喜歡他們會看起來像這樣
餅乾
- 像
- 踩
蛋糕
- 像
- 踩
等。但是在我的它看上去就像這樣:
餅乾
- 像
餅乾
- 踩
代碼好的,這裏是StackOverflow人員的代碼來翻譯appart,並向我解釋我做錯了什麼,或者我可能錯過了什麼,或者我應該怎麼做才能使它按照它的意圖執行。
<?php
//create_cat.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,
forums.forum_admin,
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>';
if ($_SESSION['user_level'] != 9 AND $row['forum_admin'] == 1) {
echo "<div style='padding:8px;background-color:#fae7af;'>Sorry but you this is for Admins only.</div><br>";
} else {
echo '<td class="leftpart">';
echo '<h3><a href="viewforum.php?f=' . $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?t=' . $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';
?>
SQL
**Category**
cat_id | int(8) | primary | auto incr
cat_name | var(255)
|2|Test 2
|3|Test 3
|1|Test 1
**Forum**
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|More Test | Just a 2nd test forum
|3|1|Bugs|Bugs and related go here
這就是它。
您的代碼一目瞭然。看看你的MySQL,因爲它似乎你想要的輸出不是你所得到的 - 在phpMyAdmin中運行你的SELECT來看看。 – Martin
@Martin我添加了SQL藥劑的類別和論壇 –