gpt4 book ai didi

php - 与相应的论坛循环类别一次

转载 作者:行者123 更新时间:2023-11-30 01:28:05 25 4
gpt4 key购买 nike

我想做的只是打印一次类别,然后打印他们的论坛。但反而多次打印类别和论坛......

我的 table :

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

最佳答案

您的联接缺少 USING 子句或 WHERE 子句。基本上你需要

INNER JOIN 论坛 f ON c.id = f.cat_id

关于php - 与相应的论坛循环类别一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17763716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com