Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Generate UL LI , UL LI

Can't figure out how-to generate this menu using a while-loop.

This is an example of my code:

<ul id="nav">
<li><a href="#">Hoofdmenu 1</a>
<ul class="sub">
        <li><a href="#">Submenu 1.1</a></li>
        <li><a href="#">Submenu 1.2</a></li>
        <li><a href="#">Submenu 1.3</a></li>
        <li><a href="#">Submenu 1.4</a></li>
    </ul>
</li>

<li><a href="#">Hoofdmenu 2</a>
    <ul class="sub">
        <li><a href="#">Submenu 2.1</a></li>
        <li><a href="#">Submenu 2.2</a></li>
        <li><a href="#">Submenu 2.3</a></li>
        <li><a href="#">Submenu 2.4</a></li>
    </ul>
</li>
</ul>

My dbtable looks like:

paginas:
    id
    title
    content
    type

When type == id from the parent it should be the submenu. In my example this works, now I've got to make it dynamic. Brains ain't working atm.

Thanks for your help!

Used code to get data from db:

<ul id="nav">
<?php
include_once("ond/inc/php/connect.php");
$query = "SELECT * FROM paginas WHERE type = '0'";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)){

echo '<li><a href="?ond='.$row->titel.'">'.$row->titel.'</a>';}
echo '<ul class="sub">';

$query2 = "SELECT * FROM paginas WHERE type = '".$row->id."'";
$result2 = mysql_query($query2);    
while($row2 = mysql_fetch_object($result2))
{
    echo '<li><a href="?ond='.$row2->titel.'">'.$row2->titel.'</a></li>';
}
echo '</ul>'; 
echo '</li>';

?>
</ul>

2 Answers

I would do something like this:

First, grab your data out as an array and loop through it for each entry. Then run something like this:

$menuArray = array();

if (empty($type)) // If the entry has no "type", then it's a parent
{
    $menuArray[$type]['title'] = $title;
}
else // else, it's a child, so append it to the parent
{
    $menuArray[$type]['subitems'][] = $title;
}

Then, having $menuArray, loop through it to create the menu:

?><ul id="nav"><?php
foreach ($menuArray as $item)
{
    ?><li><a href="#">$item['title']</a><?php
    ?><ul class="sub"><?php

    foreach ($item['subitems'] as $subItem)
    {
        <li><a href="#">$subItem</a></li>
    }

    ?></ul><?php
    ?></li><?php
}
?></ul><?php
like image 122
Gavin Anderegg Avatar answered Dec 01 '25 05:12

Gavin Anderegg


Next lines did the solution:

<ul id="nav">
<?php
include_once("ond/inc/php/connect.php");
$query = "SELECT * FROM paginas WHERE type = '0'";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)){

echo '<li><a href="?ond='.$row->titel.'">'.$row->titel.'</a>';

$query2 = "SELECT * FROM paginas WHERE type = '".$row->id."'";
$result2 = mysql_query($query2);    
echo '<ul class="sub">';
while($row2 = mysql_fetch_object($result2))
{   
    echo '<li><a href="?ond='.$row2->titel.'">'.$row2->titel.'</a></li>';


}
    echo '</ul>';
echo '</li>';}



?>
</ul>

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!