Jump to content

generating menus problems


divinedesigns1

Recommended Posts

so i been messing around with php to find a way to generate menus using php *im bored, so yeah better do something*

anyway i decided to do it this way

function menu() {
    include "config.php";
    $mainMenu = mysqli_query($con, "SELECT * FROM `main-menu`") or die('Error: ' . mysqli_error($con));
    if($mainMenu){
        ?>
            <ul>
        <?php
        while($mainRow = mysqli_fetch_assoc($mainMenu)){
            $menuId = $mainRow['id'];
            $commonName = $mainRow['actualName'];
            $menuName = $mainRow['name'];
            $menuTags = '<li><a href="' . $menuName . '">' . $commonName;
            echo $menuTags;
        }
        // check if there's any submenus
        $subMenu = mysqli_query($con, "SELECT * FROM `sub-menu` WHERE `sub-id`=$menuId") or die('Error: ' . mysqli_error($con));
        if($subMenu){
            while($subRow = mysqli_fetch_assoc($subMenu)){
                $subId = $subRow['sub-id'];
                $subName = $subRow['sub-name'];
                $subConame = $subRow['actualNames'];
                $subMain = $subRow['main-menu-id'];
                $actualMenu = '<ul>';
                $actualMenu .= '<li><a href="' . $subName . '">' . $subConame . '</li>';
                $actualMenu .= '</ul></li>';
                echo $actualMenu;
            }
        }
    }
    ?>
        </ul>
    <?php
}
?>

i was wonder if there is another way of doing this, because im only getting one of the sub menu instead of both of them

so currently the menu looks like this 

  • Home
  • Services
    • Service 1

it suppose to look like this instead

  • Home
  • Services
    • Service 1
    • Service 2

any idea of what im doing wrong? as always your response is appreciated greatly

Edited by divinedesigns1
Link to comment
Share on other sites

The way i did it was to have all menus in single database table, there would be auto increment id for each and every menu, also there would be another column to hold parent menu id, which could hold many identical id ref to the single auto incremental id that refers to parent, if a menu had no sub-menus it would be given a value of zero.

The loop would go through all menu id, if parent link equals 0 display single parent menu, move to next, if other than zero stop and with nested loop gather all records (submenus) whose parent menu id's equals current parent menu unique id from outer loop.

Link to comment
Share on other sites

41 minutes ago, dsonesuk said:

The way i did it was to have all menus in single database table, there would be auto increment id for each and every menu, also there would be another column to hold parent menu id, which could hold many identical id ref to the single auto incremental id that refers to parent, if a menu had no sub-menus it would be given a value of zero.

The loop would go through all menu id, if parent link equals 0 display single parent menu, move to next, if other than zero stop and with nested loop gather all records (submenus) whose parent menu id's equals current parent menu unique id from outer loop.

this sound way better than what i just did, now i just have to adjust my database to make this work, thank a lot dsonesuk

Edited by divinedesigns1
Link to comment
Share on other sites

On 10/23/2017 at 8:08 AM, dsonesuk said:

You should also consider a sort order column, which help sort the parent and sub-menus of each parent with sub-menus.

I got it working, I also try a similar outcome between a status and its comment and both seem to render find 😄 thanks for the help

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...