jagguy Posted March 26, 2007 Share Posted March 26, 2007 Hi, I am wondering how to do this efficiently in php. I have a table of data with 1 col has the category_id and another has a parent_id eg category_id category parent_id 1 hardware 0 2 DVD 0 3 action 2 4 carchase 3 5 cars 0 etc action is a subcategory of DVD and carchase is a subcategory of action. How can i list all this data in some sort of order of category with subcategories listed below each category with php/myql. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 26, 2007 Share Posted March 26, 2007 <?php function recurList($start) { global $master_array; $temp = array_keys($master_array['parent_id'],$master_array['parent_id'][$start]); if ( count($temp) > 0 ) { ?> <ul> <?php foreach($temp as $key => $val) { ?> <li><?php echo $master_array['category'][$val]; ?> <?php if ( array_keys($master_array['parent_id'],$master_array['category_id'][$val]) ) { recurList($val); } ?> </li> <?php } ?> </ul> <?php } } $qry = " SELECT * FROM `yourtable` ORDER BY `parent_id` ASC, `category_id` ASC "; $qry = mysql_query($qry); $master_array = NULL; while($row = mysql_fetch_assoc($qry)) { foreach($row as $key => $val) { $master_array[$key][] = $val; } } recurList(0); ?> Quote Link to comment Share on other sites More sharing options...
jagguy Posted March 27, 2007 Author Share Posted March 27, 2007 thanks for that i will give it a try. 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.