chokies12 Posted July 20, 2009 Share Posted July 20, 2009 hi need some help i dont know how to do this. i have list that shows my category items what i want to do know is to have and indent if that item is a sub category can someone help me..thx here's part of my code <table width="80%" cellpadding="2" cellspacing="2"> <tr> <td class="listhd" width="25">#</td> <td class="listhd" width="50">Actions</td> <td class="listhd">Category Name</td> <td class="listhd" width="50" align="center">Order </td> </tr> <?php $n_record_counter=1; foreach($result_fields as $v){ $s_bgColor=($n_record_counter%2) ? '#FFFFFF' : '#EFEFEF'; ?> <tr bgcolor="<?=$s_bgColor?>"> <td > <?=$n_record_counter++;?>.</td> <td > <a href="?a=categories&action=edit&id=<?=$v['id']?>"> <img src="images/icon_edit16x16.gif" /></a> <a href="?a=categories&action=delete&id=<?=$v['id']?>"> <img src="images/icon_delete16x16.gif" /></a> </td> <td class="rowGreen"> <?=$v['name']?> </td> <td align="center"> <?=$v['ordseqn']?></td> </tr> <?php } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/166604-need-help/ Share on other sites More sharing options...
ignace Posted July 20, 2009 Share Posted July 20, 2009 You need recursion to do so: function category_list(array $category, $indent = "\t") { //process if (is_array($value)) { category_list($value, $indent . "\t"); } } If you have 2 tables: category and subcategory. Then first retrieve all categories and just roll them out if a user clicked a category and the current category id in the loop matches the clicked category, then perform a new query which retrieves the subcategories for the active category. I strongly suggest using something differently than a table to display the indentation like a definition list (dl) a category is then (dt) and the subcategories (dd) Quote Link to comment https://forums.phpfreaks.com/topic/166604-need-help/#findComment-878529 Share on other sites More sharing options...
chokies12 Posted July 20, 2009 Author Share Posted July 20, 2009 You need recursion to do so: function category_list(array $category, $indent = "\t") { //process if (is_array($value)) { category_list($value, $indent . "\t"); } } If you have 2 tables: category and subcategory. Then first retrieve all categories and just roll them out if a user clicked a category and the current category id in the loop matches the clicked category, then perform a new query which retrieves the subcategories for the active category. I strongly suggest using something differently than a table to display the indentation like a definition list (dl) a category is then (dt) and the subcategories (dd) Hi i only have 1 table for category and the problem is this is already as is i just have to make indention for sub categories. Im just doing some updates and i not familiar with its structures im still a newbie Quote Link to comment https://forums.phpfreaks.com/topic/166604-need-help/#findComment-878541 Share on other sites More sharing options...
ignace Posted July 20, 2009 Share Posted July 20, 2009 Ok so you have one category table? Do you have a field which identifies the parent? Something like: category ---------------- id parent_id 1 0 // not a child 2 1 // child of 1 3 1 // child of 1 4 2 // child of 2 5 3 // child of 3 $currentCat = (int) $_GET['category']; while (list($id, $parent_id, ..) = mysql_fetch_array($result, MYSQL_NUM)) { if ($id === $currentCat) { $query = "SELECT * FROM categories WHERE parent_id = '$id'"; $result = mysql_query($query, $db); while (list(.. } If you are in for some complexity then take a look at the composite pattern as this will do here just nicely Quote Link to comment https://forums.phpfreaks.com/topic/166604-need-help/#findComment-878849 Share on other sites More sharing options...
chokies12 Posted July 21, 2009 Author Share Posted July 21, 2009 i hope i can follow Quote Link to comment https://forums.phpfreaks.com/topic/166604-need-help/#findComment-879464 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.