我要做的只是打印ONCE类别,然后打印论坛。但相反,在几次打印类别和论坛......
我的桌子:
categories
id | name | disp_position
forums
id | cat_id | name | description | disp_position
代码:
$lastCatID = null;
$query = $db->query("SELECT f.id AS fid, f.cat_id, f.name AS forum_name, c.name AS cat_name FROM categories c
INNER JOIN forums f
ORDER BY c.disp_position, c.id, f.disp_position");
<?php foreach($query as $row): ?>
<div class="catname">
<?php
if ($lastCatID != $row['cat_id']) {
echo '<h1>' . $row['cat_name'] . '</h1>';
$lastCatID = $row['cat_id'];
}
?>
</div>
<p><?=$row['forum_name']?></p>
<?php endforeach ?>
所以我有两个类别:
测试类别(标识1)和测试类别2(标识2)
和这些论坛:
(1, 1, 'News & Announcements', 'Official announcements are posted here.', 1),
(2, 1, 'Proposals', 'Propose and help us improve.', 5),
(3, 2, 'Gameplay', 'Talk about the game here.', 3),
(4, 2, 'Off Topic', 'Discuss all things not related to the game.', 4),
(5, 1, 'General', 'This forum is for general discussion.', 2),
(6, 1, 'Help & Support', 'Players helping players.', 6),
(7, 2, 'Bug Report', 'Found a bug? Help us squash it by reporting it here!', 7),
(8, 1, 'Trade', 'Sell your items or buy something you need.', 8);
输出:
Test category
News & Announcements
General
Test category
Gameplay
Off Topic
Test category
Proposals
Help & Support
Test category
Bug Report
Test category
Trade
News & Announcements
General
Test category 2
Gameplay
Off Topic
Test category 2
Proposals
Help & Support
Test category 2
Bug Report
Test category 2
Trade
答案 0 :(得分:0)
您的联接缺少USING子句或WHERE子句。基本上你需要
INNER JOIN forums f ON c.id = f.cat_id