deansatch Posted July 31, 2011 Share Posted July 31, 2011 I have some rows in my DB and each has a ['subpage'] value of either 0 or 1 to indicate if it is a main page(0) or subpage(1) and they are ordered so the parent is above the children e.g. 1 home (0) 2 about(0) 3 about-sub(1) 4 about-another-sub(1) 5 page(0) 6 page-sub1(1) 7 contact(0) What would be the best way to loop through these as a nested ul? I would have personally preferred to give each sub page a parent id but the person that created the CMS just did it like this so I am stuck with it! Quote Link to comment https://forums.phpfreaks.com/topic/243384-while-loop-with-nested-list/ Share on other sites More sharing options...
the182guy Posted July 31, 2011 Share Posted July 31, 2011 If a subpage doesn't have a parent ID, how are you determining which parent it is a child of? Not the PK ID ordered surely? Quote Link to comment https://forums.phpfreaks.com/topic/243384-while-loop-with-nested-list/#findComment-1249807 Share on other sites More sharing options...
deansatch Posted July 31, 2011 Author Share Posted July 31, 2011 there is a column ['itemorder'] so basically by selecting from the db and ORDER BY itemorder - stupid, I know...but I didn't write it....I just have to do the front end output. Quote Link to comment https://forums.phpfreaks.com/topic/243384-while-loop-with-nested-list/#findComment-1249809 Share on other sites More sharing options...
deansatch Posted July 31, 2011 Author Share Posted July 31, 2011 Well, I did this and it seems to work... <ul> <li><a href="/">Home</a></li> <?php $i=0; $x=0; $q = mysql_query("SELECT * FROM tbl_pages ORDER BY ITEMORDER desc"); while($r = mysql_fetch_array($q)){ $x++; if($r['SUBPAGE'] == '1'){ $i++; if($i==1){echo '<ul>';} echo "<li><a href=\"#\">".$r['TITLE']."</a></li>\n"; }else{ $i=0; if($x>1){ echo "</ul></li>\n";} echo "<li><a href=\"#\">".$r['TITLE']."</a>"; } } ?> </li></ul> Still seems a bit crappy to me though. Any better ideas? Quote Link to comment https://forums.phpfreaks.com/topic/243384-while-loop-with-nested-list/#findComment-1249818 Share on other sites More sharing options...
deansatch Posted July 31, 2011 Author Share Posted July 31, 2011 Nah! That doesn't work. It adds a closing ul in each main page li when I have no subpages! Doh! Any help please??? Quote Link to comment https://forums.phpfreaks.com/topic/243384-while-loop-with-nested-list/#findComment-1249826 Share on other sites More sharing options...
the182guy Posted July 31, 2011 Share Posted July 31, 2011 This should do it <?php $menuItems = array(); $result = mysql_query("SELECT * FROM tbl_pages ORDER BY ITEMORDER desc"); if($result && mysql_num_rows($result) > 0) { while($item = mysql_fetch_assoc($result)) { if($item['SUBPAGE'] == '1') { // subpage, so append to the newest parent $menuItems[count($menuItems)-1]['subpages'][] = $item; } else { // parent, so just add to the array $item['subpages'] = array(); $menuItems[] = $item; } } } // we now have an array of parent items, each with a list of subpages // print out the <ul> struct ?> <html> ...... <?php if(count($menuItems) > 0): ?> <ul> <?php foreach($menuItems as $menuItem): ?> <li> <?php echo $menuItem['title']; ?> <?php if(count($menuItem['subpages']) > 0): ?> <ul> <?php foreach($menuItem['subpages'] as $subItem):?> <li><?php echo $subItem['title']; ?></li> <?php endforeach; ?> </ul> <?php endif; ?> </li> <?php endforeach; ?> </ul> <?php endif; ?> .... </html> Quote Link to comment https://forums.phpfreaks.com/topic/243384-while-loop-with-nested-list/#findComment-1249851 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.