Hyaku_ Posted January 2, 2007 Share Posted January 2, 2007 Hi!I'm trying to list category tree, but I'm having problems listing sub-sub.. categories. This is my Category field:[code]+-------+----------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+----------------+| ID | int(11) | NO | PRI | NULL | auto_increment || NAME | longtext | YES | | NULL | || P_ID | int(11) | YES | | NULL | |+-------+----------+------+-----+---------+----------------+[/code]This is my code So far:[quote]function list_cat(){ $query = "SELECT * FROM CATEGORY WHERE P_ID = 0"; $query_exec = mysql_query($query); while($data = mysql_fetch_array($query_exec)){ echo $data['NAME'] . "<BR>"; $s_query = "SELECT * FROM CATEGORY WHERE P_ID = " . $data['ID']; $s_query_exec = mysql_query($s_query); while($s_data = mysql_fetch_array($s_query_exec)){ echo "<font size='1'>" . $s_data['NAME'] . "</font><br>"; } }}[/quote]For example I have these values into the database:[code]+----+-----------------+------+| ID | NAME | P_ID |+----+-----------------+------+| 1 | Animals | 0 || 2 | Dogs | 1 || 3 | Cats | 1 || 4 | Black | 2 || 5 | White | 2 |+----+-----------------+------+[/code]P_ID is Categories Parrent ID. Now my function prints this:Animals[i]Dogs[/i][i]Cats[/i]Dogs and Cats are sub-categories of Animals, but Dogs sub-categorie contains sub-sub categorie White and Black. I want to be able to print thous under Dog sub-categorie. And I want to sub-sub-sub.. categorie count to be unlimited, so I need my function to be able to always print the full categorie tree. In this case like this:Animals[i]Dogs[/i][sub][i]WhiteBlack[/i][/sub][i]Cats[/i]Any tips please?! I guess I could use arrays, but I don't have a really good plan how! Thanks! Link to comment https://forums.phpfreaks.com/topic/32620-solved-list-all-subsub-sub-etc-categories-category-tree/ Share on other sites More sharing options...
trq Posted January 2, 2007 Share Posted January 2, 2007 Id'e suggest taking a look at [url=http://www.phpriot.com/d/articles/php/application-design/nested-trees-1/index.html]Nested Sets[/url]. Link to comment https://forums.phpfreaks.com/topic/32620-solved-list-all-subsub-sub-etc-categories-category-tree/#findComment-151739 Share on other sites More sharing options...
Barand Posted January 2, 2007 Share Posted January 2, 2007 You need a recursive function[code]<?phpinclude '../test/db.php';function list_cat($parent, $level=0) { $sql = "SELECT id, name FROM categories WHERE p_id = '$parent' ORDER BY name"; $res = mysql_query($sql) or die(mysql_error()); while (list($id, $name) = mysql_fetch_row($res)) { $indent = str_repeat(' ', $level*3); echo "$indent$name<br>"; list_cat($id, $level+1); }}/*** call the function*/list_cat(0);?>[/code] Link to comment https://forums.phpfreaks.com/topic/32620-solved-list-all-subsub-sub-etc-categories-category-tree/#findComment-151783 Share on other sites More sharing options...
Hyaku_ Posted January 3, 2007 Author Share Posted January 3, 2007 Thanks alot, man!! Link to comment https://forums.phpfreaks.com/topic/32620-solved-list-all-subsub-sub-etc-categories-category-tree/#findComment-152047 Share on other sites More sharing options...
palace Posted March 7, 2007 Share Posted March 7, 2007 What does the code look like that updates the database with the category information?I'm getting my head around how the principle works but can't picture how the admin form would be like to put values into the table. Link to comment https://forums.phpfreaks.com/topic/32620-solved-list-all-subsub-sub-etc-categories-category-tree/#findComment-202025 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.