0

I have nav menu in html and i want to create a loop everything is fine but the item that have children echo twice! i know where is the problem but i can't figure out how to solve it :( my sql table named menu

and this is my php:

      $db = mysqli_connect('localhost', 'root', 'password', 'aftab');
<?php
$get = mysqli_query($db , "SELECT * from menu where parent_id is NULL");
while ($rowmenu = mysqli_fetch_assoc($get)) {
    echo '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-656"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ;
    $id = $rowmenu['id'] ;
    $check = mysqli_query($db , "SELECT * from menu where parent_id = '$id'");
    if ( mysqli_num_rows($check) ) { 
         echo '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-656"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ; 
         echo '<ul class="sub-menu">' ;
        while ( $row2 = mysqli_fetch_assoc($check) ) {
            echo '<li class="menu-item-302"><a href="' . $row2['link'] . '">' . $row2['name'] . '</a></li>' ;
        }
        echo '</ul>' ;
    } else {
        echo '</li>' ;
    }
}

?>

and this the result:enter image description here

i know it happen because the father item that hold the sub menu's called in $get once and another time when it need other css class.i tried if , foreach , while and many things. i need that item that holds submenus should have "menu-item-has-children" class otherwise its not show the sub menus.

1
  • Please, you have to avoid greetings and any kind of salutations. Commented May 10, 2020 at 17:35

3 Answers 3

1

When a menu item, have childrens then echo menu with menu-item-has-children class, otherwise echo a simple menu and move on.

<?php

$get = mysqli_query($db , "SELECT * from menu where parent_id is NULL");
while ($rowmenu = mysqli_fetch_assoc($get)) {
    $id = $rowmenu['id'] ;
    $check = mysqli_query($db , "SELECT * from menu where parent_id = '$id'");
    $haveSubMenu = mysqli_num_rows($check);

    if($haveSubMenu)
        echo '<li class="menu-item menu-item-type-post_type menu-item-object-page  menu-item-656 menu-item-has-children"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ;
    else
        echo '<li class="menu-item menu-item-type-post_type menu-item-object-page  menu-item-656"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ;

    if ($haveSubMenu) 
    { 
        echo '<ul class="sub-menu">' ;
        while ( $row2 = mysqli_fetch_assoc($check) ) {
           echo '<li class="menu-item-302"><a href="' . $row2['link'] . '">' . $row2['name'] . '</a></li>' ;
        }
        echo '</ul>' ;
    } else {
        echo '</li>' ;
    }
}

?>
Sign up to request clarification or add additional context in comments.

Comments

0

Here is see that Our Products from the database is printed twice.This is because you are fetching and echoing $row2['name'] twice but you can handle the first coming array by changing your SQL query.

Change your first SQL query to

$get = mysqli_query($db , "SELECT * from menu where parent_id is NULL AND name != 'OUR PRODUCTS'");

Querying this to database you will get all the NULL values in the array excluding OUR PRODUCTS and thus it will be echoed only once by the second query.

3 Comments

thank you but it doesn't work actually it removed both "OUR PRODUCT" because it no more available in $id = $rowmenu['id'] ; $check = mysqli_query($db , "SELECT * from menu where parent_id = '$id'");
i didn't get you?
@rezarezakhani restate your problem on the question?
-1

I tried the above to answer all is working only a few level dropdown menu.

If you want to really create your menu for multi-level dropdown as like tree structure just use the bellow function. for more detailing once go through

https://bootstrapfriendly.com/blog/dynamic-multi-level-dropdown-sticky-menu-in-php-mysql-using-bootstrap/

<?php
include_once("connection.php");
$query = "SELECT id, label, link, parent FROM menus ORDER BY  sort ASC, label";
$result = mysqli_query($conn, $query) or die("database error:" . mysqli_error($conn));
// Create an array to conatin a list of items and parents
$menus = array(
    'items' => array(),
    'parents' => array()
);
// Builds the array lists with data from the SQL result
while ($items = mysqli_fetch_assoc($result)) {
    // Create current menus item id into array
    $menus['items'][$items['id']] = $items;
    // Creates list of all items with children
    $menus['parents'][$items['parent']][] = $items['id'];

    //echo  $items;
}

// function to create dynamic treeview menus
function createMenu($parent, $menu)
{
    $html = "";
    if (isset($menu['parents'][$parent])) {
        // $html .= '<ul class="sina-menu sina-menu-right" data-in="fadeInLeft" data-out="fadeInOut">';
        foreach ($menu['parents'][$parent] as $itemId) {
            if (!isset($menu['parents'][$itemId])) {
                $html .= "<li > 
                         <a  href='" . $menu['items'][$itemId]['link'] . "'>" . $menu['items'][$itemId]['label'] . "</a> 
                     </li>";
            }
            if (isset($menu['parents'][$itemId])) {
                $html .= "<li class='dropdown'> 
                  <a class='dropdown-toggle' data-toggle='dropdown' href='" . $menu['items'][$itemId]['link'] . "'>" . $menu['items'][$itemId]['label'] .  "</a>";
                $html .= '<ul class="dropdown-menu">';
                $html .= createMenu($itemId, $menu);
                $html .= '</ul>';

                $html .= "</li>";
            }
        }
        // $html .= "</ul>";
    }
    return $html;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.