gpt4 book ai didi

php - 从多个 mysql 表构建 bootstrap 4 导航菜单

转载 作者:行者123 更新时间:2023-11-29 05:54:54 24 4
gpt4 key购买 nike

我想制作一个 bootstrap 4 导航菜单,我有以下 SQL 查询,下面有一些代码,但我不知道如何执行此操作!

这些是表格

TABLE menu
--------------------------------------
| id | title | url |
| 1 | Home | index.php |
| 2 | Menu | # |
| 3 | Contact | # |
| 2 | Winkelwagen | winkelwagen.php |
--------------------------------------

TABLE categories
-------------------------------------
| id | title_cat | url | cparent_id |
| 1 | Auto's | # | 2 |
| 2 | Drank | # | 2 |
-------------------------------------

TABLE products
-------------------------------------
| id | product | url | pparent_id |
| 1 | Ferrari | # | 1 |
| 2 | Heineken | # | 2 |
-------------------------------------

这里是查询:

$query =    "SELECT
X.level,
X.id,
X.name,
X.url,
X.parent_id
FROM
(
SELECT
1 AS LEVEL,
id AS id,
title AS NAME,
url AS url,
0 AS parent_id,
id AS id_1,
-1 AS id_2,
-1 AS id_3
FROM
menu
WHERE
1
UNION
SELECT
2 AS LEVEL,
id AS id,
title_cat AS NAME,
url AS url,
cparent_id AS parent_id,
cparent_id AS id_1,
id AS id_2,
-1 AS id_3
FROM
categories
WHERE
1
UNION
SELECT
3 AS LEVEL,
products.id AS id,
products.product AS NAME,
products.url AS url,
products.pparent_id AS parent_id,
categories.cparent_id AS id_1,
categories.id AS id_2,
products.id AS id_3
FROM
products
LEFT JOIN categories ON products.pparent_id = categories.id
WHERE
1
) X
WHERE
1
ORDER BY
id_1,
id_2,
id_3";

它给出了下表的级别(我也添加了 parent_id,但是 parent_id buildTree($array) 进入循环):

level   id  name        url             parent_id   
1 1 Home index.php 0
1 2 Menu # 0
2 1 Auto's # 2
3 1 Ferrari # 1
2 2 Drank # 2
3 2 Heineken # 2
1 3 Contact contact.php 0
1 4 Winkelwagen winkelwagen.php 0

我希望导航菜单看起来像这样:

        <li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Menu</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<div class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="#">Auto's</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Ferrari</a>
</div>
</div>
<div class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="#">Drank</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Heineken</a>
</div>
</div>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="winkelwagen.php">Winkelwagen</a>
</li>

我有以下代码,首先我们根据您在上面看到的获取的查询创建一个数组:

$sql = $pdo->prepare($query);

function menu_builder($sql) {
if ($sql->execute()) {
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
$array[] = $row;
}
buildTree($array); // or menu_builder($sql);
}
}

下一个代码不起作用,因为它进入了无限循环(如果它起作用,我仍然需要使 html 正确 :):

function buildTree($array, $parent_id = 0, $parents = array()) {
if($parent_id == 0) {
foreach ($array as $element) {
if (($element['parent_id'] != 0) && !in_array($element['parent_id'], $parents)) {
$parents[] = $element['parent_id'];
}
}
}
$menu_html = '';
foreach($array as $element) {
if($element['parent_id'] == $parent_id) {
if(in_array($element['id'], $parents)) {
$menu_html .= '<li class="dropdown">';
$menu_html .= '<a href="'.$element['url'].'" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">'.$element['name'].' <span class="caret"></span></a>';
}
else {
$menu_html .= '<li>';
$menu_html .= '<a href="' . $element['url'] . '">' . $element['name'] . '</a>';
}
if(in_array($element['id'], $parents)) {
$menu_html .= '<ul class="dropdown-menu" role="menu">';
$menu_html .= buildTree($array, $element['id'], $parents);
$menu_html .= '</ul>';
}
$menu_html .= '</li>';
}
}
return $menu_html;
}

而这个是正常的 <ul>/<li>我不知道如何使用 Bootstrap 让它为我工作的菜单:

function menu_builder($sql) {
$level = 0;
if ($sql->execute()) {
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
while($level < $row['level']) {
echo "<ul>" . PHP_EOL;
$level++;
}
while($level > $row['level']) {
echo "</ul>" . PHP_EOL;
$level--;
}
echo " <li>#" . $row['id'] . "->" . $row['name'] . "</li>" . PHP_EOL;
}
}
while($level-- > 0) {
echo "</ul>" . PHP_EOL;
}
}

如果您需要更多信息,请问我,我尝试使用我正在尝试的代码和我正在使用的表格尽可能清楚地说明问题。

jQuery:

$('.dropdown-menu a.dropdown-toggle').on('click', function(e) {
if (!$(this).next().hasClass('show')) {
$(this).parents('.dropdown-menu').first().find('.show').removeClass("show");
}
var $subMenu = $(this).next(".dropdown-menu");
$subMenu.toggleClass('show');
$(this).parents('li.nav-item.dropdown.show').on('hidden.bs.dropdown', function(e) {
$('.dropdown-submenu .show').removeClass("show");
});
return false;
});

CSS:

.dropdown-submenu {
position: relative;
}

.dropdown-submenu a::after {
transform: rotate(-90deg);
position: absolute;
right: 6px;
top: .8em;
}

.dropdown-submenu .dropdown-menu {
top: 0;
left: 100%;
margin-left: .1rem;
margin-right: .1rem;
}

最佳答案

我选择了一种比递归 buildTree 更线性的方法。由于需要根据树的级别输出不同的 HTML,因此这种方式更容易一些。我创建了一个 SQLFiddle对于您的数据,我为测试目的添加了一些额外的值。查询发生变化,以便我可以在一行中查看菜单项是否有子菜单以及该子菜单是否有产品:

SELECT m.title AS title, m.url AS m_url,
c.title_cat AS title_cat, c.url AS c_url,
p.product AS product, p.url AS p_url
FROM menu m
LEFT JOIN categories c
ON c.cparent_id = m.id
LEFT JOIN products p
ON p.pparent_id = c.id
ORDER BY m.id, c.id, p.id

此查询的输出(基于扩展数据)是:

title           m_url           title_cat   c_url   product     p_url
Home index.php (null) (null) (null) (null)
Menu # Auto's # Ferrari www.ferrari.com
Menu # Auto's # Maserati #
Menu # Drank # Heineken #
Menu # Food # (null) (null)
Second Menu # Hotels # The Ritz www.ritzparis.com
Contact contact.php (null) (null) (null) (null)
Winkelwagen winkelwagen.php (null) (null) (null) (null)

基本的查询调用保持不变,但我不是获取所有数据然后处理它,而是同时获取数据和处理它。

$sql = $pdo->prepare($query);
$sql->execute() or die("Unable to execute query!");
buildTree($sql);

buildTree 例程。我认为它的注释相当不言自明,但基本上它会遍历每一行数据并确定是否需要依次创建新菜单项、新子菜单或新子菜单项。

function buildTree($sql) {
$thisTitle = '';
$thisCategory = '';
while ($element = $sql->fetch(PDO::FETCH_ASSOC)) {
if (!$element['c_url']) {
// simple top element
// do we need to close any prior menus?
if ($thisCategory != '') {
echo " </div>\n </div>\n";
$thisCategory = '';
}
if ($thisTitle != '') {
echo "</li>\n";
$thisTitle = '';
}
echo <<<EOD
<li class="nav-item">
<a class="nav-link" href="{$element['m_url']}">{$element['title']}</a>
</li>

EOD;
}
else {
// got a category
// do we need a new menu item?
if ($element['title'] != $thisTitle) {
// is it the first menu item? if not, need to close the previous one
if ($thisTitle != '') {
// do we also need to close a previous category menu?
if ($thisCategory != '') {
echo " </div>\n </div>\n";
$thisCategory = '';
}
echo "</li>\n";
}
$thisTitle = $element['title'];
echo <<<EOD
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="{$element['m_url']}" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">$thisTitle</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">

EOD;
}
// do we need a new submenu?
if ($element['title_cat'] != $thisCategory) {
// is it the first submenu? if not, need to close the previous one
if ($thisCategory != '') echo " </div>\n";
$thisCategory = $element['title_cat'];
// create a submenu
echo <<<EOD
<div class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="{$element['c_url']}">$thisCategory</a>

EOD;
}
// is there a product?
if ($element['p_url']) {
// create a product menu item
echo <<<EOD
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{$element['p_url']}">{$element['product']}</a>
</div>

EOD;
}
}
}
}

此代码对扩展数据的输出是:

<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Menu</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<div class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="#">Auto's</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="www.ferrari.com">Ferrari</a>
</div>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Maserati</a>
</div>
</div>
<div class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="#">Drank</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Heineken</a>
</div>
</div>
<div class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="#">Food</a>
</div>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Second Menu</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<div class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="#">Hotels</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="www.ritzparis.com">The Ritz Paris</a>
</div>
</div>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.php">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="winkelwagen.php">Winkelwagen</a>
</li>

关于php - 从多个 mysql 表构建 bootstrap 4 导航菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50355024/

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