mort Posted September 3, 2006 Share Posted September 3, 2006 Hey allI am making a category system, but cant quite figure out how to code an unlimited array of sub-categories, and also how to display them. in reality it probably wont go more than 5 or so deep, but it would be nice to make it infinite just incasethe categories table has 3 fields. cat_id, cat_name, parent_id (the cat_id of the category it's in)if someone could just point me in the right direction i should be coolcheers ;D Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 3, 2006 Share Posted September 3, 2006 for loop Quote Link to comment Share on other sites More sharing options...
mort Posted September 3, 2006 Author Share Posted September 3, 2006 lol ok so maybe a little more descriptuive than thatany idea how to build the array of categories etc? anyone else done what i am looking to do could lend some pointers? Quote Link to comment Share on other sites More sharing options...
radar Posted September 3, 2006 Share Posted September 3, 2006 I would like to know how to do this as well.. Great Question.. In my example I will have the categories for Tutorials such as Photoshop, Illustrator, PHP, ASP, CSS, JavaScript, ETC...But then some of them may have sub categories such as for Photoshop might have..Text EffectsPhoto ManipulationBrushingetc...That'd be a good thing to know.. Quote Link to comment Share on other sites More sharing options...
mort Posted September 3, 2006 Author Share Posted September 3, 2006 if you just want categories with 1 sub-category then its easyjust multiple sub-cats r proving difficult Quote Link to comment Share on other sites More sharing options...
Barand Posted September 3, 2006 Share Posted September 3, 2006 You need a recursive function EG[code]<?phpinclude 'db.php';function listSubCats ($parent, $level = 0) { $sql = "SELECT category_id, name FROM categories WHERE parent = '$parent' "; $res = mysql_query($sql) or die(mysql_error()); while (list($id, $name) = mysql_fetch_row($res)) { $indent = str_repeat('-- --', $level); echo "$indent $name<br />"; listSubCats ($id, $level+1); // now list subcats of this }}listSubCats(0); // assuming top level have parent id = 0 ?>[/code] Quote Link to comment Share on other sites More sharing options...
mort Posted September 3, 2006 Author Share Posted September 3, 2006 ahh mate at first glance that looks perfect!!gonna try it out. swear i tried putting a function inside itself before and it cried lolanyways nice one :) Quote Link to comment Share on other sites More sharing options...
Barand Posted September 3, 2006 Share Posted September 3, 2006 Note - I wrote for my own test table and col names slightly different Quote Link to comment Share on other sites More sharing options...
mort Posted September 3, 2006 Author Share Posted September 3, 2006 ahh dude you've only gone and done it!!exactly what i was after, a nice little function, brilliant :)cheers man! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.