-1

I'm beginner and I'm trying to make a simple navigation menu with a dropdown but the first loop stops when the nested loop is completed. Is there any suggestion? Please be analytic because of my low programming skills.

<?php

function nav_main($dbc, $path) {

    $q = "SELECT * FROM navigation ORDER BY position ASC";
    $r = mysqli_query($dbc, $q);

    while($nav = mysqli_fetch_assoc($r)) {

        $nav['slug'] = get_slug($dbc, $nav['url']);

        if($nav['parent_id'] == 0) {

?>  

    <li<?php selected($path['call_parts'][0], $nav['slug'], ' class="active"') ?>><a href="<?php echo $nav['url']; ?>"><?php echo $nav['label']; ?></a></li>

<?php 
        } 
        elseif ($nav['parent_id'] == 1) {
 ?>

    <li class="dropdown<?php selected($path['call_parts'][0], $nav['slug'], ' active"') ?>"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php echo $nav['label']; ?><span class="caret"></span></a>
    <ul class="dropdown-menu">

<?php 

           while($subnav = mysqli_fetch_assoc($r)) {

               $subnav['slug'] = get_slug($dbc, $subnav['url']);

               if($subnav['parent_id'] == 3) {

?>      
                <li><a href="<?php echo $subnav['url']; ?>"><?php echo $subnav['label']; ?></a></li>
                <li role="separator" class="divider"></li>
<?php 
                } 
            } 
?>

            </ul>
          </li>

 <?php 
         } 
     } 
}  
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Bob Cat
  • 9
  • 4
  • 2
    There is only **one** `while` in your code... – FirstOne Mar 01 '16 at 13:53
  • 1
    Your code would be easier to read for us and you if you indent it and put close braces on their own line...also we can't see what the 'elseif{}' block is a continuation of. – Hektor Mar 01 '16 at 13:55
  • This is a common mistake I've seen sometimes now. **When the second loop reaches the end, the resultset is also in the end**, so there is nothing else left for the first loop to use... [This page](http://stackoverflow.com/questions/6439230/how-to-go-through-mysql-result-twice) is about that issue (but note that in that question, it's about `mysql_*` **not** `mysqli_*`. Edit: I suggest you to loop the resultset once and save the values into another array, then you can loop it any number of times you want.. – FirstOne Mar 01 '16 at 13:57

1 Answers1

0

Both your while loops are processing the same result set!

So obviously when the second while loop finishes the resultset is completely consumed and there is nothing left for the outer while look to do but terminate.

This is a precise of what you are doing :-

$q = "SELECT * FROM navigation ORDER BY position ASC";
$r = mysqli_query($dbc, $q);

// first while loop
while($nav = mysqli_fetch_assoc($r)) {

    // inner while loop
    while($subnav = mysqli_fetch_assoc($r)) {

Both are using $r and therefore regardless of you using a different variable to hold the returned row data the inner loop will consume row 2->end of the same result set as is being processed in the outer while loop

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • You are right my friend I just fixed with a second result. Thank you. – Bob Cat Mar 01 '16 at 14:06
  • [This answer](http://stackoverflow.com/a/6631631/4577762) shows a solution that consists of saving the values in an array, just don't forget to adapt to `mysqli_*`. Also, since this uses inner and outer loops, remember to use different variable names with this approach... – FirstOne Mar 01 '16 at 14:06
  • Is it worth highlighting that the code from this answer shouldn't be used? Just a thought, cause people might copy it without even reading the explanation that lays right after it (I know I might xD) – FirstOne Mar 01 '16 at 14:37
  • @FirstOne They are out there, and they do mingle with us almost unnoticed, but I doubt anything I can do will mitigate against a hasty viewing by one of them! – RiggsFolly Mar 01 '16 at 14:56