Jump to content

Need: Recursive function for menue


Fotographer96

Recommended Posts

I asked you to "output" your tables (i.e. to a text file) not attach an image. If you want my help, at least take the time to provide the information to me so I don't have to manually enter records.

 

Anyway, after making the correction I had previously asked you to make, the results I get are correct. However, I found one possible problem. If a parent does not have any children associated with it, the parent was not included in the output. I assume that is contrary to what you want. This is easily fixed by making the join a LEFT JOIN. But, that created another issue where a record may not have child data. So, I added another condition to only display the child records if the data isn't empty. Lastly, I added some "tabs" into the HTML code to provide some structure in  the output.

 

The code

mysql_connect($__DB_SERVER__, $__DB_USER__, $__DB_PASS__) or die(mysql_error());
mysql_select_db($__DB_NAME__) or die(mysql_error());

$query = "SELECT pp.ID, pp.Name, p.pageName, p.pageID
          FROM parent_pages AS pp
          LEFT JOIN pages AS p ON p.parentID = pp.ID
          ORDER BY pp.Name";
$result = mysql_query($query) or die(mysql_query());

//Open the parent UL
$menuHTML = "<ul>\n";
$currentParentID = false;
while ($row = mysql_fetch_assoc($result))
{
    if($currentParentID !== $row['ID'])
    {
        //This is a new parent page
        if($currentParentID!==false)
        {
            //If not first parent, close last submenu UL and parent LI
            $menuHTML .= "\t\t</ul>\n";
            $menuHTML .= "\t</li>\n";
        }
        $currentParentID = $row['ID'];
        //Open new parent LI, create link and open submenu UL
        $menuHTML .= "\t<li class=\"mainnav\">\n";
        $menuHTML .= "\t\t<a rel=\"nofollow\" href=\"view.php?id={$row['ID']}\">{$row['Name']}</a>\n";
        $menuHTML .= "\t\t<ul>\n";
    }

    //Create submenu LI (if exists)
    if(!empty($row['pageID']))
    {
        $menuHTML .= "\t\t\t<li class=\"subnav\">";
        $menuHTML .= "<a rel=\"nofollow\" class=\"sublink_nav\" href=\"viewpage.php?pageid={$row['pageID']}\">{$row['pageName']}</a>";
        $menuHTML .= "</li>\n";
    }
}
//Close last child UL, parent LI and parent UL
$menuHTML .= "\t\t</ul>\n";
$menuHTML .= "\t</li>\n";
$menuHTML .= "</ul>\n";

echo $menuHTML;

 

The output

<ul>
   <li class="mainnav">
      <a rel="nofollow" href="view.php?id=2">Group</a>
      <ul>
      </ul>
   </li>
   <li class="mainnav">
      <a rel="nofollow" href="view.php?id=3">Spa</a>
      <ul>
      </ul>
   </li>
   <li class="mainnav">
      <a rel="nofollow" href="view.php?id=1">Stre</a>
      <ul>
         <li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=1">Site 1</a></li>
      </ul>
   </li>
   <li class="mainnav">
      <a rel="nofollow" href="view.php?id=4">Well</a>
      <ul>
         <li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=3">Site 3</a></li>
         <li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=2">Site 2</a></li>
      </ul>
   </li>
</ul>

 

Here is an image of the rendered output (note I used some slightly different data since you made me type it in).

phpfreaks_333662.jpg

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.