我正在尝试做的只是打印类别 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 ?>
所以我有2个类别:
测试类别 (id 1) 和测试类别 2 (id 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