Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/243384-while-loop-with-nested-list/
Share on other sites

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?

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.