UnknownPlayer Posted May 25, 2011 Share Posted May 25, 2011 How can i make unlimited sub categories when i have this mysql fields: id, name, parent $query = "SELECT * FROM cats"; $result = mysql_query($query); while ($var = mysql_fetch_array($result)) { echo $var['name']; // now i need here some code for sub cats, but sub cat in subcat } Can someone help me? Link to comment https://forums.phpfreaks.com/topic/237479-unlimited-sub-categories/ Share on other sites More sharing options...
btherl Posted May 26, 2011 Share Posted May 26, 2011 The right approach depends on what you are doing. Are you trying to display the entire category tree in a single page? If so you will need recursion. A good start would be to write a function called "get_subcats($id)" which recursively calls itself to get subcats of subcats. Link to comment https://forums.phpfreaks.com/topic/237479-unlimited-sub-categories/#findComment-1220317 Share on other sites More sharing options...
UnknownPlayer Posted May 27, 2011 Author Share Posted May 27, 2011 Yes i wonna to display like a tree, can you give me example? Link to comment https://forums.phpfreaks.com/topic/237479-unlimited-sub-categories/#findComment-1221441 Share on other sites More sharing options...
btherl Posted May 29, 2011 Share Posted May 29, 2011 The code is simpler if you use multiple queries. First make a function: get_subcats($parent) { $query = "SELECT * FROM cats WHERE parent = '" . mysql_real_escape_string($parent) . "'"; $result = mysql_query($query) or die("Error in $query: " . mysql_error()); while ($var = mysql_fetch_array($result)) { echo $var['name']; get_subcats($var['id']); } } I'm assuming your parent column is an id number. Also the exact code depends on what value your top level nodes have for the parent column - is this null, 0 or some other value? Link to comment https://forums.phpfreaks.com/topic/237479-unlimited-sub-categories/#findComment-1222045 Share on other sites More sharing options...
whisperer19 Posted May 30, 2011 Share Posted May 30, 2011 Check this out http://www.phpfreaks.com/forums/index.php?topic=205633.0 it works fine. Just use the code of the last post Link to comment https://forums.phpfreaks.com/topic/237479-unlimited-sub-categories/#findComment-1222178 Share on other sites More sharing options...
btherl Posted May 30, 2011 Share Posted May 30, 2011 FYI the code posted by whisperer19 only allows one level of subcategories Link to comment https://forums.phpfreaks.com/topic/237479-unlimited-sub-categories/#findComment-1222182 Share on other sites More sharing options...
whisperer19 Posted May 30, 2011 Share Posted May 30, 2011 FYI the code posted by whisperer19 only allows one level of subcategories No, I've tested the code and it supports unlimited subcategories Link to comment https://forums.phpfreaks.com/topic/237479-unlimited-sub-categories/#findComment-1222185 Share on other sites More sharing options...
btherl Posted May 30, 2011 Share Posted May 30, 2011 It needs a bit more than what's in that thread to support unlimited subcategories. The code there has no recursion, which is the key idea needed to make this work. Link to comment https://forums.phpfreaks.com/topic/237479-unlimited-sub-categories/#findComment-1222189 Share on other sites More sharing options...
whisperer19 Posted May 30, 2011 Share Posted May 30, 2011 Last week I was looking for a function that will return a tree structure of categories and unlimited subcategories, from a table with just 3 basic fields: id, category & parent_category. This is the most simple code (very clever) that I found and it works fine. Test it and you'll see that you can have unlimited subcategories. Link to comment https://forums.phpfreaks.com/topic/237479-unlimited-sub-categories/#findComment-1222195 Share on other sites More sharing options...
btherl Posted May 30, 2011 Share Posted May 30, 2011 I'm not sure we're talking about the same thing here. I'm talking about a tree with unlimited depth, such as DMOZ. Link to comment https://forums.phpfreaks.com/topic/237479-unlimited-sub-categories/#findComment-1222198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.