Jump to content

Problem building menu from mysql and do-while


andresfz87

Recommended Posts

I have a table in my db with all my menus and submenues of my site, I'd like to show them in some kind of menu containing all of them

each entry in db has the columns ID,NAME,TEXT,LEVEL,DEPENDENT

level has a value of 1 or 2 depending if it's an item or a subitem

dependent has a reference to the id of the section it belongs to

 

Using dreamweaver I created two recordsets based on the level, they are called row_sections (only where level = 1) and row_subsections (only where level = 2)

 

here is the code I tried to use:

 

    <?php do { ?>
      <p><a href="test.php?sec=<?php echo $row_sections['id'];?>"><?php echo $row_sections['name']; ?></a></p>  //prints the section text
                    <?php do { ?>
                    <?php
                    if($row_subsections['dependent'] == $row_sections['id']) {
                    echo $row_subsections['name']."<br>"; //tries to print all subitems corresponding to the actual item
                    }
                    ?>
                    <?php } while ($row_subsections = mysql_fetch_assoc($subsections)); ?>
            <?php } while ($row_sections = mysql_fetch_assoc($sections)); ?>

 

Here's the output:

 

section 1 title

 

subsection 1

subsection 2

 

section 2 title

 

both section 1 and 2 should have two different subitems.. where is the problem?

 

if I add the line

<?php echo $row_sections['id']; ?>

right before the second

<?php do{ ?>

it shows the corresponding ID for BOTH sections so I can't understand the problem...

in your code all do loops should be while loops

 

while ($row_subsections = mysql_fetch_assoc($subsections)) {
     // do something with $row_subsections
}

 

otherwise, $row_subsections is not set to anything the first time through. i thought it odd, which is why i guessed dreamweaver did it. i leave dreamweaver as soon as design is finished.

I tried only with the code you sugested (please correct me if i'm wrong):

 

<?php while ($row_sections = mysql_fetch_assoc($sections)){ ?>
<p><a href="test.php?sec=<?php echo $row_sections['id'];?>"><?php echo $row_sections['name']; ?></a></p>
  <?php echo $row_sections['id']; ?> //this is to show current id to see if it works
<?php } ?>

 

Instead of looping it seems to skip to the last record (section 2 title) and show it as the only result, should show 2 records.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.