1

If i were to fetch my navigation items from a database for my navigation bar, like the ff: Select * FROM table WHERE linkcatagory = 'main-menu'; << this will list all the main menu items, what if one of those items has a sub-items that also has to be fetched from the database. I also declared a linkcategory for my sub-menus. So the logic is when i hovered through the main items, there sub menus will be fetched from the database as well. How can I make it work?

The code below is my sample markup but i don't think it's correct.

<?php include "includes/config.php";
$sql = "Select * FROM sites WHERE site_category = 'main-menu'";
$result = mysql_query($sql);
?>
<header>
<a href="index.php" class="logo"><h1>Website</h1></a><!--LOGO-->
<div id="gutter"></div><!--gutter-->
    <nav>
     <ul id="menu">
        <?php while($record = mysql_fetch_array($result)){

        $sitename = $record['site_name'];
        $sitelink = $record['site_link'];

        if($sitename == 'products'){
        ?>
        <li><a href="<?= $sitelink ?>" onMouseOver=""><?= $sitename ?></a>

            <ul class="submenu">
                <?php 
                    $sql = "Select * FROM sites WHERE 
                                             site_category = 'sub-menu'";
                    $result = mysql_query($sql);

                    while($record = 
                                            mysql_fetch_array($result)){
                ?>
                    <li><a href="<?= $sitelink ?>"><?= 
                                            $sitename ?></a></li>

                    <?php
                    }//while close 
                    ?>
            </ul>
        </li>
    <?php
    }else{
        echo "<li><a href=".$record['site_link'].">".$record['site_name']."
                         </a></li>";
    }
      }//while close?>
     </ul><!--menu-->
    </nav><!--NAV-->
</header><!--HEADER-->
3
  • You need to build a db structure which includes menu_id&menu_parent_id and then fetch all menu items at once and then process them with a self calling function to build your menu structure with hierarchy... if you like i can post the one i use but it's a little bit complicated -i use different stylings controlled by another parameter- Commented Nov 24, 2012 at 2:57
  • Ok then here you are my friend ... phpfiddle.org/lite/code/fb4-6uc Commented Nov 24, 2012 at 13:31
  • If you have any problems with any of sample links in answer or with fiddle above just drop a line , i will explain as i can Commented Nov 27, 2012 at 1:45

1 Answer 1

0

As i commented i can't provide you the function i use but there are many answers given for your -possible duplicate- question ;

PHP/MySQL Navigation Menu

Recursive menu with php and MySQLi

Display tree menu of selected parent

Hierarchical recursion menu with PHP/MySQL

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

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.