Hi,
I am having trouble with a recursive function to produce a nested HTML list. I’ve tried a bunch of things and have come close, but alas…no success. I was hoping someone here could help me.
I have an array of Message objects. Each Message object can have multiple children but does not necessarily have any. I want to make a nested list of message titles.
Message methods:
// returns true if it has any children, false otherwise
$message->hasChildren()
// returns a message object of the next child. $message now has one less child.
$message->nextChild()
// display the message title
$message->title
How to make a nested list:
<ul>
<li>
Parent 1
<ul>
<li>
Child 1 of Parent 1
<ul>
<li>Grandchild 1 of Parent 1</li>
<li>Grandchild 2 of Parent 1</li>
</ul>
</li>
<li>
Child 2 of Parent 1
</li>
</ul>
</li>
<li>
Parent 2
</li>
</ul>
Thanks!
EDIT:
This actually works, but it constructs a separate list for each parent. Not exactly what I wanted but it will probably do.
function threadHTML($parent) {
$html = '<ul style="margin-top: 0; margin-bottom: 0;">';
$html .= '<li>';
$html .= $parent->title;
while($parent->hasChildren()) {
$child = $parent->nextChild();
$html .= threadHTML($child);
}
$html .= '</li>';
$html .= '</ul>';
return $html;
}
$html = '';
foreach($messages as $message) {
$html .= threadHTML($message);
}
echo $html;