Page 1 of 1

Nested dynamic menu PROBLEM

Posted: Thu Jul 23, 2009 3:09 am
by mathruD
I'm trying to create a vertical, nested, dynamic menu, but I'm having a small problem. Only the submenu for the first title in the main navigation is showing. Here is the code I have so far:

<div id="contentleft"> //div that holds the navigation

<ul>
<?php do { ?>
<li><?php echo $row_nav_rs['nav_title']; ?></li> //output main level navigation

<ul>
<?php do { ?>
<li><?php echo $row_subnav_rs['nav_title']; ?></li> //list submenu for main level navigation if available
<?php } while ($row_subnav_rs = mysql_fetch_assoc($subnav_rs)); ?></ul>

<?php } while ($row_nav_rs = mysql_fetch_assoc($nav_rs)); ?></ul> //loop back through the main navigation until all titles are posted

</div>


The do loop works fine but only shows the submenu for the first title in the main navigation. This is what the output looks like:

FRUIT
>>apples
>>oranges
>>bananas
VEGETABLES
//should show squash here but doesn't show anything
MEAT
//should show steak here but doesn't show anything
DAIRY
BREAD


If anyone could help me out here it would be greatly appreciated. Thanks.

Re: Nested dynamic menu PROBLEM

Posted: Thu Jul 23, 2009 3:17 am
by onion2k
That code is pretty hard to read.. let's reformat it!

Code: Select all

<div id="contentleft"> //div that holds the navigation
<?php
 
echo "<ul>";
 
do {
 
  echo "<li>";
  echo $row_nav_rs['nav_title'];
  echo "</li>"; //output main level navigation
 
  echo "<ul>";
  do {
    echo "<li>";
    echo $row_subnav_rs['nav_title'];
    echo "</li>"; //list submenu for main level navigation if available
  } while ($row_subnav_rs = mysql_fetch_assoc($subnav_rs));
  echo "</ul>";
 
} while ($row_nav_rs = mysql_fetch_assoc($nav_rs)); //loop back through the main navigation until all titles are posted
 
echo "</ul>";
 
?>
</div>
How does the inner loop know which sub navigation resultset ($subnav_rs) to loop through?

Re: Nested dynamic menu PROBLEM

Posted: Thu Jul 23, 2009 4:10 am
by mathruD
Sorry about the code being hard to read. The attached image shows how I have the database set up. There are two recordsets in the actual PHP file (nav_rs and subnav_rs)

In the nav_rs recordset I displayed the main navigation by setting it to only display those results in which nav_subid = 0

In the subnav_rs recordset I displayed any subnavigation by setting it to only display those results in which nav_subid = nav_id

As far as the inner loop knowing which results to pull from I don't know exactly what I'm doing wrong. It seems as though the subnav loop is only being executed for the first main navigation result. Should I possibly create some sort of variable, after the echo of the main navigation, and set it equal to nav_id? That way maybe nav_subid would know what id to pull from. Or should I write some sort of IF statement to run the check.

I'm still somewhat new to this and this is the first dynamic nested menu I've created so I'm a bit confused at the moment.

Re: Nested dynamic menu PROBLEM

Posted: Thu Jul 23, 2009 2:58 pm
by mathruD
^^bump

Can someone please help me out here?

Re: Nested dynamic menu PROBLEM

Posted: Thu Jul 23, 2009 4:08 pm
by omniuni
I'm wondering if it has something to do with ID's. You mention it being "dynamic" so if you're using JS, it sounds similar to a problem I had when I tried using the same ID for multiple items. Make sure any id="blah" tags are using unique "blah"s

Re: Nested dynamic menu PROBLEM

Posted: Thu Jul 23, 2009 6:21 pm
by mathruD
i'm not using any js in the menu at the moment. i'm just trying to pull my results from the database. it's pulling fine, but it seems as though the check for sub navigation only executes for the first menu item. after that, the rest of the menu items are listed, but the check for submenu items isn't executed.

Re: Nested dynamic menu PROBLEM

Posted: Thu Jul 23, 2009 9:15 pm
by omniuni
Oh, I'm sorry. I'm not sure what's going on. :?

Re: Nested dynamic menu PROBLEM

Posted: Fri Jul 24, 2009 1:35 am
by mathruD
thanks for all the replies, but i finally got it to work. the recordset had to be moved inside the "do" loop for the main navigation. now the submenu shows for all of the main navigation titles.

Code: Select all

<div id="contentleft">
 
<ul>
  <?php do { ?>
    <li><?php echo $row_nav_rs['nav_title']; ?></li>
    <?php $var_temp = $row_nav_rs['nav_id']; ?>            [color=#40BF00]//i don't know that this temp var is necessarily needed but it worked[/color]
    
    
<?php mysql_select_db($database_artdept, $artdept);
$query_subnav_rs = "SELECT * FROM nav WHERE nav_subid = $var_temp";
$subnav_rs = mysql_query($query_subnav_rs, $artdept) or die(mysql_error());
$row_subnav_rs = mysql_fetch_assoc($subnav_rs);
$totalRows_subnav_rs = mysql_num_rows($subnav_rs);
 
?>
    
<ul>
  <?php do { ?>
  <li><?php echo $row_subnav_rs['nav_title']; ?></li>
  <?php } while ($row_subnav_rs = mysql_fetch_assoc($subnav_rs)); ?></ul>
  
  
  
  <?php } while ($row_nav_rs = mysql_fetch_assoc($nav_rs)); ?></ul>
 
</div>