Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Menu from MySQL

  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Menu from MySQL

    I am trying to automate menu creation, I'm still somewhat new at this, I managed to get a navigation system to work, (index.php?sectionid=xx&pageid=xx). I'm pretty sure I have no idea here and am completely off but here goes...
    PHP Code:
    <?php
    $G1 
    mysql_query('SELECT * FROM section WHERE category = 1');
    $G2 mysql_query('SELECT * FROM section WHERE category = 2');
    $GRAB mysql_query('SELECT * FROM nav WHERE sectionid = "'$sectid'"');
    ?>

    <div id="leftnav">
    <?php

    while($nav mysql_fetch_array($G1)) {
        
    $sectid $nav['sectionid'];
        
    $titlea $nav['title'];
        echo 
    '<div class="box"><div class="boxtitle">'$titlea .'</div><ul>';
        while(
    $nava mysql_fetch_array($GRAB)) {
        echo 
    '<li><a href="#">'$nava['name'] .'</a></li>';
        }
        echo 
    '</ul></div>';
    }


    ?>
    </div>

    <div id="rightnav">
    <?php

    while($nav mysql_fetch_array($G2)) {
        
    $sectid=$nav['sectionid'];
        
    $titlea $nav['title'];
        echo 
    '<div class="box"><div class="boxtitle">'$titlea .'</div><ul>';
        while(
    $nava mysql_fetch_array($GRAB)) {
            echo 
    '<li><a href="#">'$nava['name'] .'</a></li>';
        }
        echo 
    '</ul></div>';
    }


    ?>
    </div>
    I got all the boxtitles and boxes to show up, but the list items do not :| Database is simple now, until I get it to work...
    table 'section' has 3 columns: "category", "sectionid", "title"
    table 'nav' has 2 columns: "sectionid" & "name"

    I am trying to implement this completely wrong?

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $GRAB = mysql_query('SELECT * FROM nav WHERE sectionid = "'. $sectid. '"');

    this line at top sectid is not set till futher down really needs to be

    in while loop which starts around line 28


    while($nav = mysql_fetch_array($G2)) {
    $sectid=$nav['id'];
    $GRAB = mysql_query('SELECT * FROM test WHERE id = "'. $sectid. '"');

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic Menu Class

    Dynamic Menus are the way to go! Here's a Menu Class I put together and have been using for a long time. Hope someone finds it useful.

    -Hawkmoon

    PHP Code:
    <?
    /*
    //############################## NOTES ###############################################
    Works from MENU table which is
    MENUID int(11) NOT NULL AUTO_INCREMENT PRI,     //Unique ID
    MENUITEM varchar(100) NOT NULL,                 //Display Name for menu item
    MENUFILE varchar(100) NOT NULL,                 //File for href
    GID int(11)                                        //1-admin 2-superuser 3-client
    ORDERID int(11)                                    //Order for menu items
    ICON varchar(100) NOT NULL,                     //File for icon
    */

    //############################## objMenu Class ######################################
    class objMenu {
        var 
    $database;
        var 
    $gid;
        var 
    $arrMenu = array();
        
        function 
    objMenu($database,$gid) {
            
    $this->database $database;
            
    $this->gid        $gid;
            
    $this->setMenu();
        }
        
        function 
    getMenu($menuType) {
            
    $str  "";
            
    $str .= "<table border=0 cellpadding=0 cellspacing=0>";
            foreach(
    $this->arrMenu as $row) {
                list(
    $menuid,$menuitem,$menufile,$orderid,$icon) = $row;
                
    //Currently items with an $orderid over 100 create a different menu
                //To alter the menu length change the numbers below.
                
    if($menuType=="right") {
                    
    $tempHigh    "200";
                    
    $tempLow    "100";
                } else {
                    
    $tempHigh    "100";
                    
    $tempLow    "0";
                }
                if(
    $orderid>$tempLow && $orderid<$tempHigh) {
                    
    $str .= "<tr><td>";
                        if(
    $icon!="") {
                            
    $str .= "<a href='$menufile'><img src='$icon' border=0></a>";
                        } else {
                            
    $str .= "<a href='$menufile' class='menu' border=0>$menuitem</a>";
                        }
                    
    $str .= "</td></tr>";
                }
            }
            
    $str .= "</table>";
          return 
    $str;
        }
        
        function 
    setMenu() {
            
    $sql  "SELECT MENUID, MENUITEM, MENUFILE, ORDERID, ICON";
            
    $sql .= "    FROM $this->database";
            
    $sql .= "    WHERE GID='$this->gid'";
            
    $sql .= "    ORDER BY ORDERID ASC";
            
            
    $result        mysql_query($sql) or DIE("ERROR:" mysql_error());
            while(
    $row mysql_fetch_row($result)) {
                
    $this->arrMenu[] = $row;
            }
        }

    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •