我想在PHP中生成多級菜單的列表,但是我沒有如何做到這一點,我想用簡單的示例幫助我並對類別和子類進行排序類別與他們的頭銜,這樣的事情:使用PHP對數組進行排序以生成多級菜單
<ul>
<li>Category 1</li>
<li>
<h4>TITLE HERE</h4>
<ul>
<li>Category Child 1</li>
<li>Category Child 2</li>
<li>Category Child 3</li>
<li>
<h4>TITLE HERE</h4>
<ul>
<li>Category child 1</li>
<li>Category child 2</li>
<li>Category child 3</li>
</ul>
</li>
</ul>
</li>
<li>Category 1</li>
<li>Category 2</li>
<li>Category 3</li>
</ul>
我在PHP的數組:
$data = Array (
'0' => Array (
'id' => '1',
'id_parent' => '',
'name' => 'Sports'
),
'1' => Array (
'id' => '2',
'id_parent' => '1',
'name' => 'Football'
),
'2' => Array (
'id' => '3',
'id_parent' => '1',
'name' => 'Bascket'
),
'3' => Array (
'id' => '4',
'id_parent' => '',
'name' => 'Health'
),
'4' => Array (
'id' => '5',
'id_parent' => '4',
'name' => 'Nutrition and diet'
),
'5' => Array (
'id' => '6',
'id_parent' => '4',
'name' => 'Beauty Salon'
),
'6' => Array (
'id' => '7',
'id_parent' => '',
'name' => 'Films'
),
'7' => Array (
'id' => '8',
'id_parent' => '7',
'name' => 'Armageddon'
),
'8' => Array (
'id' => '9',
'id_parent' => '7',
'name' => 'Apocalypse'
),
'9' => Array (
'id' => '10',
'id_parent' => '',
'name' => 'News'
),
'10' => Array (
'id' => '11',
'id_parent' => '10',
'name' => 'International'
),
'11' => Array (
'id' => '12',
'id_parent' => '11',
'name' => 'News from Syria'
),
'12' => Array (
'id' => '13',
'id_parent' => '11',
'name' => 'News from Palestine'
)
);
我有一個表在MySQL:
CREATE TABLE `categories` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`id_parent` mediumint(8) unsigned DEFAULT NULL,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `FK_categories_UNIQUE` (`name`),
KEY `FK_categories` (`id_parent`),
CONSTRAINT `FK_categories` FOREIGN KEY (`id_parent`) REFERENCES `categories` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of categories
-- ----------------------------
INSERT INTO `categories` VALUES ('1', null, 'Sports');
INSERT INTO `categories` VALUES ('2', '1', 'Football');
INSERT INTO `categories` VALUES ('3', '1', 'Bascket');
INSERT INTO `categories` VALUES ('4', null, 'Health');
INSERT INTO `categories` VALUES ('5', '4', 'Nutrition and diet');
INSERT INTO `categories` VALUES ('6', '4', 'Beauty Salon');
INSERT INTO `categories` VALUES ('7', null, 'Films');
INSERT INTO `categories` VALUES ('8', '7', 'Armageddon');
INSERT INTO `categories` VALUES ('9', '7', 'Apocalypse');
INSERT INTO `categories` VALUES ('10', null, 'News');
INSERT INTO `categories` VALUES ('11', '10', 'International');
INSERT INTO `categories` VALUES ('12', '11', 'News from Syria');
INSERT INTO `categories` VALUES ('13', '11', 'News from Palestine');
非常感謝您的幫助。
嗎? – user10089632
@ Lambda7如果您想在HTML結構中顯示 –
您的類別中的層數是否有限制?如果是這樣,你可以通過SQL獲取記錄,並按順序。然後做一個分類來生成HTML輸出。 –