Jump to content

recursive menu unsorted list little problem


C2K

Recommended Posts

I have a little problem with my code, the output is wrong, not what I want...:

I'm using this now:

[code]<?php

function recursie($var){
    $query2 = "SELECT * FROM ddc_menu_bck WHERE sitemap = 'Y' AND siteID = '". $_GET['siteID'] ."' AND subid = '". $var ."'";
    $query1 = mysql_query($query2) or die(mysql_error());
 
    if (mysql_num_rows($query1) > 0 )
    {
    echo "<ul id=\"nav\">";
    while($query = mysql_fetch_object($query1)){
        echo "<li>". "<a href=\"" . $query->menuPag . "?mID=" . $query->mID . "&subID=" . $query->subID . "&siteID=" . $query->siteID . "\">" . $query->menuNaam ."</a>" . "</li>";
        recursie($query->mID);
    }
    echo "</ul>";
    }
}
?> [/code]

Output:

[code]<ul id="nav">
  <li>home</li>
  <li>about us</li>
      <ul id="nav">
        <li>more about us</li>
      </ul>[/code]

but I want this as an output?

[code]
<ul id="nav">
  <li>home</li>
  <li>about us
      <ul>
        <li>more about us</li>
      </ul>
  </li>[/code]

How to get this right??

I was trying also this:

[code]<?php

function recursie($var, $level = 0)
{
    $query2 = "SELECT * FROM ddc_menu_bck WHERE sitemap = 'Y' AND siteID = '". $_GET['siteID'] ."' AND subid = '". $var ."'";
    $query1 = mysql_query($query2) or die(mysql_error());
 
    if (mysql_num_rows($query1) > 0 )
    {
      if($level == 0)
        echo "<ul id=\"nav\">";
      else
        echo "<ul>";
      while($query = mysql_fetch_object($query1))
      {
          echo "<li>". "<a href=\"" . $query->menuPag . "?mID=" . $query->mID . "&subID=" . $query->subID . "&siteID=" . $query->siteID . "\">" . $query->menuNaam ."</a>" . "</li>";
          recursie($query->mID, $level++);
      }
      echo "</ul>";
    }
}

?> [/code]
The 2nd one should work as long as you are passing the correct $level values to the function. I allways prefer to put my if/else code within curly braces myself. Since level would be false (i.e. 0) for anything other than the top level you could change the condition to just (!$level)

      if(!$level) { echo "<ul id=\"nav\">"; }
      else { echo "<ul>"; }
[quote author=mlacy03 link=topic=118368.msg483631#msg483631 date=1165963106]
If you are using the IDs purely for formatting, CSS2 has a :first-child selector you might find useful:
[url=http://www.w3.org/TR/1998/REC-CSS2-19980512/selector.html#first-child]Selectors, First-Child[/url]
[/quote]

Use the ID's for main and parent selctors for the unsorted list?!
I was thinking of something like:
[code]
<style>
div > ul#nav:first-child { background-color:red; }
</style>

<div> // needed for ul to be first-child of, nothing more
<ul id=nav>
  <li>Test</li>
  <li>
        <ul>
              <li>Second Level</li>
              <li>Test</li>
        </ul>
  </li>
</ul>
</div>[/code]

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.