带排序的动态多级菜单 - 如何找出菜单级别 - PHP,MySQL

我有一个多级菜单,一个带排序的子菜单。我需要找出菜单级别,以便我可以将一些字符指定为项目符号。

我想使用选择和添加来列出它

lvl2 -<option>str_repeat ("&nbsp;", 2)也很好

lvl3 -<option>str_repeat ("&nbsp;", 3)也很好

lvl4 -<option>str_repeat ("&nbsp;", 4)也很好

知道如何做到这一点吗?

我在lv2获得了最大

CREATE TABLE `menu` (

  `id` int(11) UNSIGNED NOT NULL,

  `title` varchar(50) NOT NULL,

  `parent` int(11) UNSIGNED DEFAULT NULL,

  `page` varchar(45) DEFAULT NULL,

  `sort_order` tinyint(4) NOT NULL DEFAULT '100',

  `lang` varchar(5) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;



INSERT INTO `menu` (`id`, `title`, `parent`, `page`, `sort_order`, `lang`) VALUES

(1, 'Home', NULL, 'index.php', 1, ''),

(2, 'Products', NULL, NULL, 2, ''),

(3, 'Services', NULL, NULL, 1, ''),

(4, 'About', NULL, 'about.php', 100, ''),

(6, 'Service 1', 3, 'service1.php', 110, ''),

(7, 'Service 2', 3, 'service2.php', 100, ''),

(8, 'Product 1', 2, 'product1.php', 100, ''),

(9, 'Product 2', 2, 'product2.php', 100, ''),

(10, 'Product 3', 2, 'product3.php', 100, ''),

(11, 'Product 4', 2, 'product4.php', 100, ''),

(12, 'Product 5', 2, 'product5.php', 50, ''),

(14, 'Contact', NULL, 'contact.php', 100, ''),

(15, 'Service 1.1', 6, 'service1.1.php', 100, ''),

(16, 'Service 1.2', 6, 'service1.2.php', 100, ''),

(17, 'Service 1.1.1', 15, NULL, 100, ''),

(18, 'Service 2.1', 7, NULL, 100, ''),

(19, 'Service 2.2', 7, NULL, 100, ''),



ALTER TABLE `menu`

  ADD PRIMARY KEY (`id`),

  ADD KEY `menu_id` (`parent`);


ALTER TABLE `menu`

  MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=23;



ALTER TABLE `menu`

  ADD CONSTRAINT `menu_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `menu` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

COMMIT;

PHP


public function displayMenu($parent = null)

{

    if ($parent == null) {

        $query = $this->menuManager->getPublicMenus()->where('parent', null)->order('sort_order');

    } else {

        $query = $this->menuManager->getPublicMenus()->where('parent = ?', $parent)->order('sort_order');

    }



四季花海
浏览 85回答 1
1回答

HUWWW

最好的选择是在数据库的字段中指示级别,而不是一遍又一遍地动态计算它们。如果这不是一个选择,那么......我们将结果准备到带有idfor 索引键的数组中并添加一个level字段:$menu = [];foreach($query as $row) {&nbsp; &nbsp; $row->level = null;&nbsp; &nbsp; $menu[$row->id] = $row;}一旦完成,剩下的就很简单了:foreach($menu as &$item) {&nbsp; &nbsp; if (is_null($item->parent)) {&nbsp; &nbsp; &nbsp; &nbsp; $item->level = 1;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $item->level = $menu[$item->parent]->level + 1;&nbsp; &nbsp; }}换句话说:如果父项是null,则该项目的级别为 1。否则,它是其父项的级别 + 1。这会产生以下(显示相关字段)数组:array(17) {&nbsp; &nbsp; [1] · object(stdClass)#2 (7) {&nbsp; &nbsp; &nbsp; &nbsp; ["title"] · string(4) "Home"&nbsp; &nbsp; &nbsp; &nbsp; ["parent"] · NULL&nbsp; &nbsp; &nbsp; &nbsp; ["level"] · int(1)&nbsp; &nbsp; } ...&nbsp; &nbsp; [6] · object(stdClass)#6 (7) {&nbsp; &nbsp; &nbsp; &nbsp; ["title"] · string(9) "Service 1"&nbsp; &nbsp; &nbsp; &nbsp; ["parent"] · int(3)&nbsp; &nbsp; &nbsp; &nbsp; ["level"] · int(2)&nbsp; &nbsp; } ...&nbsp; &nbsp; [15] · object(stdClass)#14 (7) {&nbsp; &nbsp; &nbsp; &nbsp; ["title"] · string(11) "Service 1.1"&nbsp; &nbsp; &nbsp; &nbsp; ["parent"] · int(6)&nbsp; &nbsp; &nbsp; &nbsp; ["level"] · int(3)&nbsp; &nbsp; } ...&nbsp; &nbsp; [17] · object(stdClass)#16 (7) {&nbsp; &nbsp; &nbsp; &nbsp; ["title"] · string(13) "Service 1.1.1"&nbsp; &nbsp; &nbsp; &nbsp; ["parent"] · int(15)&nbsp; &nbsp; &nbsp; &nbsp; ["level"] · int(4)&nbsp; &nbsp; }}然后你就可以简单地做str_repeat ("&nbsp;", $item['level'])。不过,这里有一个需要注意的地方:如果您的菜单项“乱序”,即它将不起作用。如果在其父母之前有一个孩子。
打开App,查看更多内容
随时随地看视频慕课网APP