www.webdeveloper.com
Results 1 to 4 of 4

Thread: Can anyone see the problems with this code as I just cant see it?

  1. #1
    Join Date
    Feb 2012
    Posts
    102

    Can anyone see the problems with this code as I just cant see it?

    I wrote this code for a nav menu and I am getting some unexpected results. Here is the code.

    PHP Code:
    <?php
    // Script used to dynamically generate navigation manu for 

        
    $q "SELECT link, name FROM top_menu";
        
    $r mysqli_query($dbc$q);

        
    $menu mysqli_fetch_array($rMYSQLI_ASSOC);

        
        
            echo 
    '<nav>';
                echo 
    '<ul id="menu">';
                    echo 
    '<li><a href="' $menu[0]['link'] . '" title="' $menu[0]['name'] . '">' $menu[0]['name'] . '</a></li>'// Home Link
                    
    echo '<li><a href="' $menu[1]['link'] . '" title="' $menu[1]['name'] . '">' $menu[1]['name'] . '</a>'// Top Ten Travel List Link
                        
    echo '<ul id="first-sub-menu">';
                            
    $qs "SELECT * FROM state_menu WHERE category_id=2";
                            
    $rs mysqli_query($dbc$qs);
                            while (
    $state_menu mysqli_fetch_array($rsMYSQLI_ASSOC)) {
                                
                                echo 
    '<li><a href="'.$state_menu['link'].'" title="'.$state_menu['name'].'">'.$state_menu['name'].'</a></li>';
                                
                            }
                        echo 
    '</ul>';
                    echo 
    '</li>';
                    echo 
    '<li><a href="'.$menu[2]['link'].'" title="'.$menu[2]['name'].'">'.$menu[2]['name'].'</a></li>'// Travel Blog Link
                    
    echo '<li><a href="'.$menu[3]['link'].'" title="'.$menu[3]['name'].'">'.$menu[3]['name'].'</a>'// Resturant Menu
                        
    echo '<ul id="first-sub-menu">';
                            
    $qrs "SELECT DISTICNT s.link, s.name, s.state_id FROM state_menu AS s LEFT JOIN resturant_menu AS r USING (state_id) WHERE r.state_id=s.state_id AND category_id=3 ORDER BY s.state_id";
                            
    $rrs mysqli_query($dbc$qrs);
                            while (
    $state_menu_rr mysqli_fetch_array($rrsMYSQLI_ASSOC)) {
                            
                                echo 
    '<li><a href="'.$state_menu_rr['link'].'" title="'.$state_menu_rr['name'].'">'.$state_menu_rr.'</a>';
                                    echo 
    '<ul id="second-sub-menu">';
                            
                                        
    $qrm "SELECT link, name FROM resturant_menu WHERE state_id={$state_menu_rr['state_id']}";
                                        
    $rr mysqli_query($dbc$qrm);
                                        while (
    $resturant_menu mysqli_fetch_array($rrMYSQLI_ASSOC)) {
                                        
                                            echo 
    '<li><a href="rr.php'.$resturant_menu['link'].'" title="'.$resturant_menu['name'].'">'.$resturant_menu['name'].'</a></li>';
                                        }
                                    echo 
    '</ul>';
                                echo 
    '</li>';
                            }
                        echo 
    '</ul>';
                    echo 
    '</li>';
                    echo 
    '<li><a href="'.$menu[4]['link'].'" title="'.$menu[4]['name'].'">'.$menu[4]['name'].'</a>'// Hotel Menu
                        
    echo '<ul id="first-sub-menu">';
                            
    $qhs "SELECT DISTICNT s.link, s.name, s.state_id FROM state_menu AS s LEFT JOIN hotel_menu AS h USING (state_id) WHERE h.state_id=s.state_id AND category_id=4 ORDER BY s.state_id";
                            
    $hrs mysqli_query($dbc$qhs);
                            while (
    $state_menu_hr mysqli_fetch_array($hrsMYSQLI_ASSOC)) {
                            
                                echo 
    '<li><a href="'.$state_menu_hr['link'].'" title="'.$state_menu_hr['name'].'">'.$state_menu_hr.'</a>';
                                    echo 
    '<ul id="second-sub-menu">';
                                    
                                        
    $qhm "SELECT link, name FROM hotel_menu WHERE state_id={$state_menu_hr['state_id']}";
                                        
    $hr mysqli_query($dbc$qhm);
                                        while (
    $hotel_menu mysqli_fetch_array($hrMYSQLI_ASSOC)) {
                                        
                                            echo 
    '<li><a href="hr.php'.$hotel_menu['link'].'" title="'.$hotel_menu['name'].'">'.$hotel_menu['name'].'</a></li>';
                                        }
                                    echo 
    '</ul>'// Closing the second-sub-menu.
                                
    echo '</li>';  // Closing the link from the state menu.
                            
    }
                        echo 
    '</ul>';
                    echo 
    '</li>';
                    echo 
    '<li><a href="'.$menu[5]['link'].'" title="'.$menu[5]['name'].'">'.$menu[5]['name'].'</a></li>';
                echo 
    '</ul>';
            echo 
    '</nav>';
    Here is a screen shot of what I am getting:

    Attachment 15367

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    From the way you are using $menu as a 2-D array, it appears to me that this...
    PHP Code:
        $menu mysqli_fetch_array($rMYSQLI_ASSOC); 
    ...should instead be...
    PHP Code:
        $menu mysqli_fetch_all($rMYSQLI_ASSOC); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2012
    Posts
    155
    Unless I am missing something, why would you not put the first query result through a while loop as well?
    PHP Code:
        
            
    echo '<nav>';
                echo 
    '<ul id="menu">';
    while (
    $menu mysqli_fetch_array($rMYSQLI_ASSOC)) {
                    echo 
    '<li><a href="' $menu['link'] . '" title="' $menu['name'] . '">' $menu['name'] . '</a></li>';
    .
    .
    .


  4. #4
    Join Date
    Feb 2012
    Posts
    102
    I cant use a while statement for the first part because if I do it will repeat everything below it and there are other pages that need to be inserted under different links.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles