gpt4 book ai didi

PHP MySQL 菜单排序

转载 作者:行者123 更新时间:2023-11-29 03:03:07 25 4
gpt4 key购买 nike

好的,这是我的表结构:

+--------------------------+ +----------------+ +-------------------------------+
| pages | | menus | | menu_pages |
+--------------------------+ +----+-----------+ +-------------------------------+
| id | title | slug | | id | name | | menu_id | page_id | parent_id |
+-------+---------+--------+ +----+-----------+ +---------+---------+-----------+
| 1 | Home | index | | 1 | default | | 1 | 1 | 0 |
+-------+---------+--------+ +----+-----------+ +---------+---------+-----------+
| 2 | About | about | | 2 | footer | | 1 | 2 | 0 |
+-------+---------+--------+ +----+-----------+ +---------+---------+-----------+
| 3 | Test 1 | test-1 | | 1 | 3 | 2 |
+-------+---------+--------+ +---------+---------+-----------+
| 4 | Test 2 | test-2 | | 1 | 4 | 2 |
+-------+---------+--------+ +---------+---------+-----------+
| 5 | Test 3 | test-3 | | 1 | 5 | 4 |
+-------+---------+--------+ +---------+---------+-----------+

基本上,我们有页面、菜单和一个 menu_pages 链接表,它指定菜单、页面和每个菜单项的父级。

这是我的查询:

$query = "SELECT pages.id, pages.title, pages.slug, menu_pages.parent_id
FROM menus, pages, menu_pages WHERE menus.name = '$menu'
AND menus.id = menu_pages.menu_id
AND pages.id = menu_pages.page_id";

$results = $db->Query($query);

这里的问题是:如何将菜单项正确地嵌套在数组中各自的父项下?我已经尝试了很多东西,但没有一个能超过简单的 2 个级别,所以我不会用它来混淆问题。显然,我需要在 PHP 中进行某种递归,或者以某种方式修改我的查询,以便让 SQL 正确返回它们?

输出应该是这样的:

[0] => array(
'id' => 1,
'title' => 'Home',
'slug' => '/',
'parent_id' => '0'
)
[1] => array(
'id' => 2,
'title' => 'About',
'slug' => 'about',
'parent_id' => 0,
'sub_menu' => array(
[0] => array(
'id' => 3,
'title' => 'Test 1',
'slug' => 'test-1',
'parent_id' => 2
)
[1] => array(
'id' => 4,
'title' => 'Test 2',
'slug' => 'test-2',
'parent_id' => '2',
'sub_menu' => array(
[0] => array(
'id' => 5,
'title' => 'Test 3',
'slug' => 'test-3',
'parent_id' => 4
)
)
)
)
)

感谢您的帮助!

最佳答案

这并不像乍听起来那么简单 - 如果您想了解如何使用 SQL 执行此操作,您正在寻找递归 SQL 语句。

不幸的是,mysql 不直接支持它,您需要编写一段代码才能使其正常工作。这里有一个如何做的例子。不简单。

如果您对如何区分它感兴趣,您可以像这样在 Oracle 中实现它:

SELECT p.id, p.title, p.slug, mp.parent_id, level
FROM menu_pages mp
JOIN pages p ON ( p.id = mp.page_id )
JOIN menus m ON ( m.id = mp.menu_id )
CONNECT BY PRIOR mp.page_id = mp.parent_id
START WITH ( m.name = 'default' AND mp.parent_id = 0 )

你基本上是在说:

  • 从对菜单顶层的查询开始
  • 通过将父级连接到子级,将其连接回结果集

你最终得到这样的结果集:

id  title   slug    parent  level
------------------------------------
1 Home index 0 1
2 About about 0 1
3 Test 1 test-1 2 2
4 Test 2 test-2 2 2
5 Test 3 test-3 4 3

除了你已经拥有的之外,这一切实际上给你的是:

  • 菜单中每个点的“级别”。
  • 如果子菜单在您的结构中多次出现,它会正确重复。
  • 未正确连接的菜单部分将不予退还。

因此,对于小的、简单的和一致的菜单来说,它可能无论如何都过头了。

此外,即使在这种情况下,您也需要在 PHP 中处理它以获得您正在寻找的结构。

因此,以此为灵感,您可以了解如何通过进行后处理在 mysql 中实现它。

您从原始查询开始:

SELECT pages.id
, pages.title
, pages.slug
, menu_pages.parent_id
FROM menus
, pages
, menu_pages
WHERE menus.name = 'default'
AND menus.id = menu_pages.menu_id
AND pages.id = menu_pages.page_id

然后您可以遍历此结果并手动构建数组结构。

为了避免递归问题,我们将利用这样一个事实,即我们可以让两个变量指向同一个数据结构——我们将使用引用,以便改变一个引用中的变量将更改另一个引用中的变量值。

即您遇到的困难是在层次结构中找到正确的点来添加每个 child 。有了引用文献,您就不必这么做了。

  • 创建一个空的菜单数组
  • 遍历 SQL 语句的结果
  • 为每个菜单项创建一个副本并将其放入一个简单索引的存储区(通过项目的 ID)
  • 如果您有根菜单项,请将其添加到您的菜单数组(作为引用)
  • 如果您没有根菜单项,请在您的简单商店中找到父菜单项并将您的新项目添加到其中。

最后你应该有你正在寻找的漂亮的嵌套结构。

像这样:

<?php

// As if it came back from mysql...
// Assumed that it's ordered so that every possible parent appears before all its childern

$aResults = array( array( 'id' => 1, 'title' => 'Home', 'slug' => 'index', 'parent_id' => 0 )
, array( 'id' => 2, 'title' => 'About', 'slug' => 'about', 'parent_id' => 0 )
, array( 'id' => 3, 'title' => 'Test 1', 'slug' => 'test-1', 'parent_id' => 2 )
, array( 'id' => 4, 'title' => 'Test 2', 'slug' => 'test-2', 'parent_id' => 2 )
, array( 'id' => 5, 'title' => 'Test 3', 'slug' => 'test-3', 'parent_id' => 4 ) );

// the menu you're creating
$aMenu = array();

// the simple store of the menu items you're going to use to find the parents
$aBaseMenuIndex = array();

foreach( $aResults as $aMenuItem ) {

$aMenuItem['sub_menu'] = array();

// add your menu item to the simple store
$aBaseMenuIndex[ $aMenuItem['id'] ] = $aMenuItem;

if ( $aMenuItem['parent_id'] == 0 ) {

// if it's a base menu item, add it to the menu
$aMenu[] =& $aBaseMenuIndex[ $aMenuItem['id'] ];

} else {

// if it's not a base item, add it to the sub menu, using the simply indexed store to find it
// adding it here will also add it to $aMenu, as $aMenu contains a reference to this
$aBaseMenuIndex[ $aMenuItem['parent_id'] ]['sub_menu'][] =& $aBaseMenuIndex[ $aMenuItem['id'] ];
}
}

var_dump( $aMenu );

关于PHP MySQL 菜单排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20092111/

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