PHPize Online / SQLize Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE `categories` ( `id` int(6) unsigned NOT NULL AUTO_INCREMENT, `parent` mediumint(8) unsigned NOT NULL DEFAULT '0', `level` tinyint(3) unsigned NOT NULL DEFAULT '0', `name` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, `description` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL, `reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=128 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `categories` -- LOCK TABLES `categories` WRITE; set autocommit=0; INSERT INTO `categories` VALUES (1,32,0,'Webadresser','','2023-03-16 13:35:21'),(2,0,0,'Aksjer - Børs','','0000-00-00 00:00:00'),(3,0,0,'Arrangement','','0000-00-00 00:00:00'),(4,0,0,'Auksjoner','','0000-00-00 00:00:00'),(5,0,0,'Banker','','0000-00-00 00:00:00'),(6,0,0,'Barn','','0000-00-00 00:00:00'),(7,48,0,'Bil','','2023-03-16 13:46:20'),(8,0,0,'Bilder/Foto','','0000-00-00 00:00:00'),(9,15,0,'Bolig','','2023-03-16 13:40:14'),(10,0,0,'Bryllup','','0000-00-00 00:00:00'),(11,48,0,'Båt','','2023-03-16 13:46:46'),(12,52,0,'Chat','','2023-03-16 13:33:09'),(13,52,0,'Dating','','2023-03-16 13:33:35'),(14,0,0,'Dyr','','0000-00-00 00:00:00'),(15,0,0,'Eiendom','','0000-00-00 00:00:00'),(16,0,0,'Familie','','0000-00-00 00:00:00'),(17,0,0,'Forsikring','','0000-00-00 00:00:00'),(18,64,0,'Fotball','','2023-03-16 12:46:43'),(19,0,0,'Friluftsliv','','0000-00-00 00:00:00'),(20,64,0,'Golf','','2023-03-16 13:19:33'),(21,40,0,'Graviditet','','2023-03-16 13:38:08'),(22,0,0,'Hagestell','','0000-00-00 00:00:00'),(23,0,0,'Helse','','2023-03-16 13:53:55'),(24,0,0,'Hobby','','0000-00-00 00:00:00'),(25,64,0,'Hockey','','2023-03-16 13:20:32'),(26,0,0,'Horoskop','','0000-00-00 00:00:00'),(27,0,0,'Hotell','','0000-00-00 00:00:00'),(28,0,0,'Humor','','0000-00-00 00:00:00'),(29,0,0,'Hus og hjem','','0000-00-00 00:00:00'),(30,15,0,'Hytte','','2023-03-16 13:40:49'),(31,64,0,'Håndball','','2023-03-16 13:20:55'),(32,0,0,'IT/data','','0000-00-00 00:00:00'),(33,0,0,'Kalkulatorer','','0000-00-00 00:00:00'),(34,0,0,'Kart','','0000-00-00 00:00:00'),(35,0,0,'Kino/film','','0000-00-00 00:00:00'),(36,0,0,'Kjendiser','','0000-00-00 00:00:00'),(37,0,0,'Konkurranser','','0000-00-00 00:00:00'),(38,52,0,'Kontaktannonser','','2023-03-16 13:37:00'),(39,0,0,'Kultur/kunst','','0000-00-00 00:00:00'),(40,0,0,'Kvinner','','0000-00-00 00:00:00'),(41,23,0,'Legemidler','','2023-03-16 13:41:55'),(42,0,0,'Leksikon','','0000-00-00 00:00:00'),(43,0,0,'Litteratur','','0000-00-00 00:00:00'),(44,0,0,'Lov og rett','','0000-00-00 00:00:00'),(45,0,0,'Lån','','0000-00-00 00:00:00'),(46,0,0,'Mat/drikke','','0000-00-00 00:00:00'),(47,32,0,'Mobil','','2023-03-16 13:56:33'),(48,0,0,'Motor','','0000-00-00 00:00:00'),(49,0,0,'Musikk','','0000-00-00 00:00:00'),(50,0,0,'Nettbutikker','','0000-00-00 00:00:00'),(51,0,0,'Nettradio','','0000-00-00 00:00:00'),(52,0,0,'Nettsamfunn','','0000-00-00 00:00:00'),(53,0,0,'Nettspill','','0000-00-00 00:00:00'),(54,0,0,'Offentlig info','','0000-00-00 00:00:00'),(55,46,0,'Oppskrifter','','2023-03-16 13:43:16'),(56,0,0,'Privatøkonomi','','0000-00-00 00:00:00'),(57,32,0,'Programvare','','2023-03-16 13:44:36'),(58,0,0,'Reise','','0000-00-00 00:00:00'),(59,58,0,'Restplasser','','2023-03-16 13:26:06'),(60,0,0,'Rubrikkannonser','','0000-00-00 00:00:00'),(61,64,0,'Ski','','2023-03-16 13:21:43'),(62,0,0,'Skole/utdanning','','0000-00-00 00:00:00'),(63,0,0,'Slektsforskning','','0000-00-00 00:00:00'),(64,0,0,'Sport','','2023-03-16 12:42:31'),(65,23,0,'Sykdommer','','2023-03-16 13:54:36'),(66,64,0,'Sykling','','2023-03-16 13:22:04'),(67,0,0,'Søkemotorer','','0000-00-00 00:00:00'),(68,0,0,'Tegneserier','','0000-00-00 00:00:00'),(69,0,0,'Telefonkataloger','','0000-00-00 00:00:00'),(70,0,0,'Tester','','0000-00-00 00:00:00'),(71,0,0,'Trening','','0000-00-00 00:00:00'),(72,0,0,'Underholdning','','0000-00-00 00:00:00'),(73,0,0,'Ungdom','','0000-00-00 00:00:00'),(74,0,0,'Valuta','','0000-00-00 00:00:00'),(75,0,0,'Veitrafikk','','0000-00-00 00:00:00'),(76,0,0,'Vitenskap','','0000-00-00 00:00:00'),(77,0,0,'Webkameraer','','0000-00-00 00:00:00'),(78,0,0,'Økonomi','','0000-00-00 00:00:00'),(79,0,0,'Vi over 60','','0000-00-00 00:00:00'),(80,18,0,'Norske klubber','','2023-03-16 12:48:15'),(81,18,0,'Engelske klubber','','2023-03-16 12:48:50'),(82,48,0,'Motorsykkel','','2023-03-16 13:51:17'),(83,0,0,'Organisasjoner','','0000-00-00 00:00:00'),(84,64,0,'Tennis','','2023-03-16 13:22:24'),(85,58,0,'Reisebrev/guider','','2023-03-16 13:26:49'),(86,58,0,'Flyreiser','','2023-03-16 13:27:11'),(87,58,0,'Båtreiser','','2023-03-16 13:27:29'),(88,76,0,'Arkeologi','Her er kategorien for de som liker å grave etter s','2023-03-16 13:11:43'),(89,76,0,'Historie','Her har vi samlet lenker som omhandler historie, s','2023-03-16 13:12:17'),(90,76,0,'Matematikk','Her finnes lenker til sider som kan være av intere','2023-03-16 13:12:47'),(91,76,0,'Astronomi','Her finner du lenker relatert til astronomi. Astro','2023-03-16 13:14:28'),(92,76,0,'Teknologi','Her finner du teknologirelaterte lenker. Teknologi','2023-03-16 13:14:54'),(93,0,0,'Museer','Her finner du lenker til museer som handler alt fr','0000-00-00 00:00:00'),(94,62,0,'Folkehøyskoler','Her finnes informasjon om folkehøgskoler og eksame','2023-03-16 13:23:15'),(95,19,0,'Fiske','Her har vi samlet lenker til alt mulig om fiske og','2023-03-16 12:51:34'),(96,19,0,'Jakt','Her har vi samlet lenker til glede for de jaktglad','2023-03-16 12:53:57'),(97,19,0,'Dykking','Her har vi samlet lenker til sider som omhandler d','2023-03-16 12:54:24'),(98,0,0,'Shopping','','0000-00-00 00:00:00'),(99,28,0,'Norsk Humor','Norsk Humor','2023-03-16 13:03:03'),(100,0,0,'Nyheter','','0000-00-00 00:00:00'),(101,0,0,'Sport','','0000-00-00 00:00:00'); UNLOCK TABLES; commit;
Copy
Copy Format Clear
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $query = $pdo->query("Select * from categories"); $category_tree = []; $root_parent = -1; $root_parent_name = ""; function makeTree($query,&$category_tree,&$root_parent,&$root_parent_name){ while($row = $query->fetch(PDO::FETCH_ASSOC)){ if(is_null($row['parent'])){ $root_parent = $row['id']; $root_parent_name = $row['name']; }else{ if(!isset($category_tree[$row['parent']])){ $category_tree[$row['parent']] = []; } $category_tree[$row['parent']][] = array($row['name'],$row['id']); } } } function buildList($category_tree,$parent){ $list = "<ul>"; foreach($category_tree[$parent] as $each_child){ $list .= "<li>" . $each_child[0]; if(isset($category_tree[$each_child[1]])){ $list .= buildList($category_tree,$each_child[1]); } $list .= "</li>"; } $list .= "</ul>"; return $list; } makeTree($query,$category_tree,$root_parent,$root_parent_name); echo "<ul>"; echo "<li>$root_parent_name"; echo buildList($category_tree,$root_parent); echo "</li>"; echo "</ul>";
Show:  
Copy