I'm trying create a page that displays a multi level menu.
For example my menu looks like this
Home
About
Product
- Watches
- Rolex
- Some other watch
- Tops
- Pants
Contact
As you can see it has more then 2 levels. What I'm trying to do is find out what level I'm on so that I can style it accordingly.
So for example Home, About, Product, Contact
would be level 1, Watches, Tops, Pants
would be level 2
and Rolex, Some other watch
would be level 3.
I would like to do this dynamically so that if I ended up having even more levels I can then style as needed. At the moment my code isn't incrementing how I would like it
This is my code
function admin_menus($parent_id = 0, $level = 1)
{
global $conn;
$query = "SELECT * FROM menus WHERE parent_id = ".$parent_id;
$menus = mysqli_query($conn, $query);
confirmQuery($menus);
// Checks to see if there is any rows in the table
if(mysqli_num_rows($menus) > 0)
{
while($row = mysqli_fetch_assoc($menus))
{
$menu_id = $row['id'];
$menu_title = $row['title'];
$menu_position = $row['position'];
if($parent_id > 0)
{
$sub_menu = "sub-menu";
$sub_icon = "<i class='fa fa-angle-right pr-10'></i>";
}else{
$sub_menu = "";
$sub_icon = "";
}
echo "<tr class='$level' data-index='$menu_id' data-position='$menu_position'>";
echo "<td>";
echo "<div class='$sub_menu'>";
echo $sub_icon;
echo "<a href='menus.php?source=edit_menu&m_id=$menu_id'>";
echo $menu_title;
echo "</a>";
echo "</div>";
echo "</td>";
echo "<td>";
echo "<div class='dropdown text-center'>";
echo "<button class='btn btn-sm btn-primary dropdown-toggle' type='button' id='dropdownActionButton' data-toggle='dropdown' aria-haspop='true' aria-expanded='false'>";
echo "<i class='fa fa-cogs'></i>";
echo "</button>";
echo "<div class='dropdown-menu dropdown-menu-right' aria-labelledby='dropdownActionButton'>";
echo "<a href='menus.php?source=edit_menu&m_id=$menu_id' class='dropdown-item'>";
echo "Edit";
echo "</a>";
echo "<a href='menus.php?delete=$menu_id' class='dropdown-item'>";
echo "Delete";
echo "</a>";
echo "</div>";
echo "</div>";
echo "</td>";
echo "</tr>";
// Increments the level
$multi_level = $level++;
admin_menus($menu_id, $level);
}
}
}