亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶排序的動態多級菜單 - 如何找出菜單級別 - PHP,MySQL

帶排序的動態多級菜單 - 如何找出菜單級別 - PHP,MySQL

PHP
四季花海 2023-09-22 16:41:30
我有一個多級菜單,一個帶排序的子菜單。我需要找出菜單級別,以便我可以將一些字符指定為項目符號。我想使用選擇和添加來列出它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;PHPpublic 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');    }
查看完整描述

1 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

最好的選擇是在數據庫的字段中指示級別,而不是一遍又一遍地動態計算它們。如果這不是一個選擇,那么......


我們將結果準備到帶有idfor 索引鍵的數組中并添加一個level字段:


$menu = [];

foreach($query as $row) {

    $row->level = null;

    $menu[$row->id] = $row;

}

一旦完成,剩下的就很簡單了:


foreach($menu as &$item) {

    if (is_null($item->parent)) {

        $item->level = 1;

    } else {

        $item->level = $menu[$item->parent]->level + 1;

    }

}

換句話說:如果父項是null,則該項目的級別為 1。否則,它是其父項的級別 + 1。這會產生以下(顯示相關字段)數組:


array(17) {

    [1] · object(stdClass)#2 (7) {

        ["title"] · string(4) "Home"

        ["parent"] · NULL

        ["level"] · int(1)

    } ...

    [6] · object(stdClass)#6 (7) {

        ["title"] · string(9) "Service 1"

        ["parent"] · int(3)

        ["level"] · int(2)

    } ...

    [15] · object(stdClass)#14 (7) {

        ["title"] · string(11) "Service 1.1"

        ["parent"] · int(6)

        ["level"] · int(3)

    } ...

    [17] · object(stdClass)#16 (7) {

        ["title"] · string(13) "Service 1.1.1"

        ["parent"] · int(15)

        ["level"] · int(4)

    }

}

然后你就可以簡單地做str_repeat ("&nbsp;", $item['level'])。不過,這里有一個需要注意的地方:如果您的菜單項“亂序”,即它將不起作用。如果在其父母之前有一個孩子。


查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 105 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號