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-->
Ajii Noguerra
  • 69
  • 1
  • 5
  • 12
  • 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- – Erdinç Çorbacı Nov 24 '12 at 02:57
  • Ok then here you are my friend ... http://phpfiddle.org/lite/code/fb4-6uc – Erdinç Çorbacı Nov 24 '12 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 – Erdinç Çorbacı Nov 27 '12 at 01:45

1 Answers1

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

Community
  • 1
  • 1
Erdinç Çorbacı
  • 1,187
  • 14
  • 17