Jump to content

Building a dynamic UL list with sublevels from array values


axtg

Recommended Posts

Hi All,

 

I've been trying to tackle this problem for some time now, and every now and then there is a point where you have to admit it is not going to work out just by yourself. Here's what I'm trying to do.

 

I've got a MySQL table with pages and their relative position indicated by db.toplevel and db.sublevel. Note that getting this information out of the database is no problem, which is why I'm posting here.

 

// Do this for five menus
for ($i = 1; $i <= '5'; $i++) { 

// Run query or die
if (!$db->Query("SELECT * FROM `pages` WHERE `published`='Y' AND `menu_id`='$i'")) $db->Kill();

// Only proceed if query returns results
if($db->RowCount() > "0") {

	// Load all variables into $row (e.g. $row[2]['toplevel'], $row[4]['sublevel'])
	$row = $db->RecordsArray();

	// Get to the <ul> generation
	/* what to do here? */
}
}

 

Some pages will have toplevel X and sublevel 0. These are parent items (sublevel = 0). Others might have toplevel X and sublevel 1 || 2 || 3, etc. Indicating child 1 || 2 || 3 of X. I'd like to have these build into a <ul> based on their order.

 

So toplevel 1, sublevel 0 should be the first item, after which all childs (1,1;1,2;1,3) follow.

 

<ul>
<li>Item 1,0
	<ul>
		<li>Item 1,1</li>
		<li>Item 1,2</li>
		<li>Item 1,3</li>
	</ul>
</li>
... etc
</ul>

 

Now I run into the problem that I can't see how to do a foreach or while, because the toplevel item will always come first. You can't open the nested <ul> tag, have the next loop add the actual <li> and then go back to close the nested <ul> after all childs have been looped through.

 

I'd figured that I could do a second SQL statement based on the current toplevel item, but somehow the while gets dirordert, which causes the while loop to break after any sublevel item (not continuing to the next toplevel item).

 

Any thoughts on how to fix this or take on the whole operation differently, will be greatly appreciated!

 

Thanks!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.