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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.