Recursive Nightmare unordered list with a twist
Posted: Mon Jun 08, 2009 7:35 am
Hi all,
I've had some luck producing recursive lists, but i seem to have pinned myself into a recursive corner I am having trouble altering elements/adding based on db fields. If anybody can point me in the right direction, i would ever so grateful.
I have a table called 'fms_parent' with the fields 'id, name, parent, header'
id is the id of the item- 0 being the root and they can stack, name is the name of the item, parent is the name of the id that the item associates with, and header is a boolean attribute that either displays the item as a LI item or as an <h3> item see below.
Sample Data
id name parent header
________________________________________________
1 Menu Item 1 0 1 (1 displays it as a header but with rules!)
2 Menu Item 2 0 1
3 Menu Item 3 0 1
4 Sub Item 1 2 1
5 Sub Item 2 2 0
6 Sub Item 3 2 0
7 Sub Sub Item 1 4 0
8 Sub Sub Item 2 4 0
The above should produce output like this:
So essentitally it is a recursive list with the following rules:
Items on the base level '0' flagged as headers show up as headers
Items flagged as headers not on 0 display as <li> elements but will create another <ul> item if they have children.
Anbody have any suggestions?
The below code is as close as i've come to, but still very far away.
I've had some luck producing recursive lists, but i seem to have pinned myself into a recursive corner I am having trouble altering elements/adding based on db fields. If anybody can point me in the right direction, i would ever so grateful.
I have a table called 'fms_parent' with the fields 'id, name, parent, header'
id is the id of the item- 0 being the root and they can stack, name is the name of the item, parent is the name of the id that the item associates with, and header is a boolean attribute that either displays the item as a LI item or as an <h3> item see below.
Sample Data
id name parent header
________________________________________________
1 Menu Item 1 0 1 (1 displays it as a header but with rules!)
2 Menu Item 2 0 1
3 Menu Item 3 0 1
4 Sub Item 1 2 1
5 Sub Item 2 2 0
6 Sub Item 3 2 0
7 Sub Sub Item 1 4 0
8 Sub Sub Item 2 4 0
The above should produce output like this:
Code: Select all
<h3>Menu Item 1</h3>
<h3>Menu Item 2</h3>
<ul>
<li>Sub Item 1
<ul>
<li>Sub Sub Item 1</li
<li>Sub Sub Item 2</li
</ul>
</li>
<li>Sub Item 2</li>
<li>Sub Item 3</li>
</ul>
<h3>Menu Item 3</h3>
Items on the base level '0' flagged as headers show up as headers
Items flagged as headers not on 0 display as <li> elements but will create another <ul> item if they have children.
Anbody have any suggestions?
The below code is as close as i've come to, but still very far away.
Code: Select all
$parentid = 0; // assuming that 0 is the main category.
get_sub_cats($parentid);
function get_sub_cats($parentid) {
$sql = "SELECT id, name, parent, header FROM fms_parent WHERE parent_id = ".$parentid."";
$run = mysql_query($sql);
echo '<ul>';
while ($rec = mysql_fetch_assoc($run)) {
echo '<li />'.$rec[name'];
get_sub_cats($rec['id']);
}
echo '</ul>';
}