首页 > 代码库 > 不遗留问题-menu数据拼装

不遗留问题-menu数据拼装

 1 DROP TABLE IF EXISTS `menu0910`; 2 CREATE TABLE `menu0910` ( 3   `id` bigint(20) NOT NULL AUTO_INCREMENT, 4   `menu` varchar(50) COLLATE utf8_unicode_ci DEFAULT ‘‘, 5   `parentid` bigint(20) DEFAULT 0, 6   PRIMARY KEY (`id`) 7 ) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 8  9 -- ----------------------------10 -- Records of menu091011 -- ----------------------------12 INSERT INTO `menu0910` VALUES (1, 1-1, 0);13 INSERT INTO `menu0910` VALUES (2, 1-2, 0);14 INSERT INTO `menu0910` VALUES (3, 1-3, 0);15 INSERT INTO `menu0910` VALUES (4, 1-1-1, 1);16 INSERT INTO `menu0910` VALUES (5, 1-1-2, 1);17 INSERT INTO `menu0910` VALUES (6, 1-1-3, 1);18 INSERT INTO `menu0910` VALUES (7, 1-2-1, 2);19 INSERT INTO `menu0910` VALUES (8, 1-2-2, 2);20 INSERT INTO `menu0910` VALUES (9, 1-2-3, 2);21 INSERT INTO `menu0910` VALUES (10, 1-3-1, 3);22 INSERT INTO `menu0910` VALUES (11, 1-3-2, 3);23 INSERT INTO `menu0910` VALUES (12, 1-3-3, 3);24 INSERT INTO `menu0910` VALUES (13, 1-1-1-1, 4);25 INSERT INTO `menu0910` VALUES (14, 1-1-1-2, 4);26 INSERT INTO `menu0910` VALUES (15, 1-1-1-3, 4);27 INSERT INTO `menu0910` VALUES (16, 1-1-2-1, 5);28 INSERT INTO `menu0910` VALUES (17, 1-1-2-2, 5);29 INSERT INTO `menu0910` VALUES (18, 1-1-2-3, 5);30 INSERT INTO `menu0910` VALUES (19, 1-1-3-1, 6);31 INSERT INTO `menu0910` VALUES (20, 1-1-3-2, 6);32 INSERT INTO `menu0910` VALUES (21, 1-1-3-3, 6);33 INSERT INTO `menu0910` VALUES (22, 1-2-1-1, 7);34 INSERT INTO `menu0910` VALUES (23, 1-2-1-2, 7);35 INSERT INTO `menu0910` VALUES (24, 1-2-1-3, 7);36 INSERT INTO `menu0910` VALUES (25, 1-2-2-1, 8);37 INSERT INTO `menu0910` VALUES (26, 1-2-2-2, 8);38 INSERT INTO `menu0910` VALUES (27, 1-2-2-3, 8);39 INSERT INTO `menu0910` VALUES (28, 1-2-3-1, 9);40 INSERT INTO `menu0910` VALUES (29, 1-2-3-2, 9);41 INSERT INTO `menu0910` VALUES (30, 1-2-3-3, 9);42 INSERT INTO `menu0910` VALUES (31, 1-3-1-1, 10);43 INSERT INTO `menu0910` VALUES (32, 1-3-1-2, 10);44 INSERT INTO `menu0910` VALUES (33, 1-3-1-3, 10);45 INSERT INTO `menu0910` VALUES (34, 1-3-2-1, 11);46 INSERT INTO `menu0910` VALUES (35, 1-3-2-2, 11);47 INSERT INTO `menu0910` VALUES (36, 1-3-2-3, 11);48 INSERT INTO `menu0910` VALUES (37, 1-3-3-1, 12);49 INSERT INTO `menu0910` VALUES (38, 1-3-3-2, 12);50 INSERT INTO `menu0910` VALUES (39, 1-3-3-3, 12);

 

 

<?php$mysqli = new mysqli(‘localhost‘, ‘root‘, ‘‘, ‘w‘);if ($mysqli->connect_errno) {    printf("Connect failed: %s\n", $mysqli->connect_error);    exit();}$menulist = array();/* Select queries return a resultset */if ($result = $mysqli->query("SELECT * FROM menu0910")) {    printf("Select returned %d rows.\n", $result->num_rows);    /* free result set */    while ($row = $result->fetch_assoc()) {        $menulist[] = $row;    }    $result->close();}var_dump($menulist);//筛选出3级idlist->2级->1级$idlist_3 = array();$idlist_2 = array();$idlist_1 = array();$idlist_1_2_3 = array();$idlist_1_2 = array();foreach($menulist as $menu){    $idlist_1_2_3[] = $menu[‘id‘];}var_dump($idlist_1_2_3);foreach($menulist as $menu){    if($menu[‘parentid‘]!=0)$idlist_1_2[] = $menu[‘parentid‘];}$idlist_1_2=array_unique($idlist_1_2);var_dump($idlist_1_2);foreach($idlist_1_2_3 as $id){    if(!in_array($id,$idlist_1_2)) $idlist_3[] = $id;}var_dump($idlist_3);foreach($idlist_3 as $id){    $idlist_2[] = get_parentid($id);}$idlist_2=array_unique($idlist_2);var_dump($idlist_2);foreach($idlist_2 as $id){    $idlist_1[] = get_parentid($id);}$idlist_1=array_unique($idlist_1);var_dump($idlist_1);function get_parentid($id){    global $menulist;    foreach($menulist as $one)    {        if($one[‘id‘]==$id) RETURN $one[‘parentid‘];    }}$res = array();foreach($idlist_1 as $one){    $res[] = $one;}var_dump($res);$w = 0;foreach($res as $id1){    $tmp = array();    foreach($idlist_2 as $id2)    {       if(get_parentid($id2) == $id1)$tmp[]=$id2;    }    $res[]=$tmp;}var_dump($res);

 

不遗留问题-menu数据拼装