gpt4 book ai didi

php - MySQL 1 到多。仅显示第一个表中的 1 个结果

转载 作者:行者123 更新时间:2023-11-29 00:56:30 26 4
gpt4 key购买 nike

示例代码

<?php
require_once('class_library/pdo.php');
$pdoConnection = new sdb('web_structure');
$select = $pdoConnection->query("SELECT main_menu.href AS main_href
, main_menu.link_name AS main_link
, sub_menu.href AS sub_href
, sub_menu.link_name AS sub_link
FROM main_menu
LEFT JOIN sub_menu ON main_menu.id = sub_menu.main_menu_id
ORDER BY main_menu.position ASC");

while($row = $select->fetch())
{
$href = $row["main_href"];
$link_name = $row["main_link"];
$sub_href = $row["sub_href"];
$sub_link_name = $row["sub_link"];

//MAIN MENU (display only once)
echo " <li><a href=\"$href\">$link_name</a>\n";

//SUB MENU (show all related results)
echo " <ul>\n";
echo " <li><a href=\"$sub_href\">$sub_link_name</a></li>\n";
echo " </ul>\n";
echo " </li>\n";
}
?>

输出

服务
- 服务 1

服务
- 服务 2 ...等等

产品
- 产品 1

产品
- 产品 2 ...等等


我愿意

服务
- 服务 1
- 服务 2

产品
- 产品 1
- 产品 2

最佳答案

<?php
require_once('class_library/pdo.php');
$pdoConnection = new sdb('web_structure');
$select = $pdoConnection->query("SELECT main_menu.href AS main_href
, main_menu.link_name AS main_link
, sub_menu.href AS sub_href
, sub_menu.link_name AS sub_link
FROM main_menu
LEFT JOIN sub_menu ON main_menu.id = sub_menu.main_menu_id
ORDER BY main_menu.position ASC, main_menu.id ASC");

$p_link_name = '';
while($row = $select->fetch())
{
$href = $row["main_href"];
$link_name = $row["main_link"];
$sub_href = $row["sub_href"];
$sub_link_name = $row["sub_link"];

//MAIN MENU (display only once)
if ($p_link_name !== $link_name) {
echo " <li><a href=\"$href\">$link_name</a>\n";
}

//SUB MENU (show all related results)
echo " <ul>\n";
echo " <li><a href=\"$sub_href\">$sub_link_name</a></li>\n";
echo " </ul>\n";
if ($p_link_name !== $link_name) {
echo " </li>\n";
}
$p_link_name = $link_name;
}
?>

关于php - MySQL 1 到多。仅显示第一个表中的 1 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5871707/

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