Jump to content

Need: Recursive function for menue


Fotographer96

Recommended Posts

Hey guys,

I've got a problem. I need a function, that gives me a menue like that:

 

parent_page1

    page1

    page2

    page3

parent_page2

    page1

    page2

    page3

 

So.. here is my database structure:

uh23zkw2.jpg

 

All what I need, is a function, that gives me a menu like that on top.

Do you need some more information? Let me know..

Link to comment
Share on other sites

Have you even attempted this on your own? you will be hard pushed to find someone who will write an entire script for you from scratch. If you have attempted this then please post some of your code

 

Yeah, I did.. but that is real mess..

 

have a look for yourself:

            <ul>
            <?php $result = mysql_query("SELECT * FROM pages INNER JOIN parent_pages ON pages.parentID=parent_pages.ID WHERE pages.parentID=parent_pages.ID");
            while ($row = mysql_fetch_assoc($result)):?>
            <li class="mainnav"><a rel='nofollow' href='view.php?id=<?php print $row['ID'];?>'><?php print $row['Name'];?></a>
                <ul>
                    <li class="subnav"><a rel='nofollow' class="sublink_nav" href='viewpage.php?pageid=<?php echo $row['pageID'];?>'><?php print $row['pageName'];?></a></li>
                </ul>
            </li>
            <?php endwhile;?>
            <?php $result = mysql_query("SELECT * FROM parent_pages WHERE pageID='0'");
            while ($row = mysql_fetch_assoc($result)):?>
            <li class="mainnav"><a rel='nofollow' href='view.php?id=<?php print $row['ID'];?>'><?php print $row['Name'];?></a></li>
            <?php endwhile;?>
            </ul>

 

I just need a small snippet.. no database connection or something else.. just the function

Link to comment
Share on other sites

try:

 <ul>
            <?php $result = mysql_query("SELECT * FROM pages INNER JOIN parent_pages ON pages.parentID=parent_pages.ID WHERE pages.parentID=parent_pages.ID");
            while ($row = mysql_fetch_assoc($result)):?>
            <li class="mainnav"><a rel='nofollow' href='view.php?id=<?php print $row['ID'];?>'><?php print $row['Name'];?></a>
                <ul>
                    <li class="subnav"><a rel='nofollow' class="sublink_nav" href='viewpage.php?pageid=<?php echo $row['pageID'];?>'><?php print $row['pageName'];?></a></li>
                </ul>
            </li>
            <?php endwhile;?>
            <?php $result2 = mysql_query("SELECT * FROM parent_pages WHERE pageID='0'");
            while ($row2 = mysql_fetch_assoc($result2)):?>
            <li class="mainnav"><a rel='nofollow' href='view.php?id=<?php print $row2['ID'];?>'><?php print $row2['Name'];?></a></li>
            <?php endwhile;?>
            </ul>

Link to comment
Share on other sites

This is the PHP Coding Help forum, where we help you with your coding.  We're not here to write code for you, nor come running when you PM random members asking for urgent help.  We certainly won't help people who demand code without demonstrating a willingness to learn.  Can you do that?

 

P.S. Welcome to PHPFreaks.

Link to comment
Share on other sites

try:

 <ul>
            <?php $result = mysql_query("SELECT * FROM pages INNER JOIN parent_pages ON pages.parentID=parent_pages.ID WHERE pages.parentID=parent_pages.ID");
            while ($row = mysql_fetch_assoc($result)):?>
            <li class="mainnav"><a rel='nofollow' href='view.php?id=<?php print $row['ID'];?>'><?php print $row['Name'];?></a>
                <ul>
                    <li class="subnav"><a rel='nofollow' class="sublink_nav" href='viewpage.php?pageid=<?php echo $row['pageID'];?>'><?php print $row['pageName'];?></a></li>
                </ul>
            </li>
            <?php endwhile;?>
            <?php $result2 = mysql_query("SELECT * FROM parent_pages WHERE pageID='0'");
            while ($row2 = mysql_fetch_assoc($result2)):?>
            <li class="mainnav"><a rel='nofollow' href='view.php?id=<?php print $row2['ID'];?>'><?php print $row2['Name'];?></a></li>
            <?php endwhile;?>
            </ul>

 

Doesn't work properly..

if I create more than one page for the parent_page, than it creates a new menu(<ul>-tag)..

 

that's why I asked you here.. I need to handle it with a recursive function..

 

This is the PHP Coding Help forum, where we help you with your coding.  We're not here to write code for you, nor come running when you PM random members asking for urgent help.  We certainly won't help people who demand code without demonstrating a willingness to learn.  Can you do that?

 

P.S. Welcome to PHPFreaks.

 

You have to understand me..

I don't know how it actually works(the recursion), before I was posting here I was looking out for tutorials.. I didn't find the key.. so I hope, that you create that function and I can see how it works(on my problem)..

that's all!

Link to comment
Share on other sites

We're not here to write code for you, nor come running when you PM random members asking for urgent help.

 

Hmm...

 

I know he must have seen salathe's post since he replied at 12:19PM. Yet, I received a PM from this person at 2:48PM.

 

@Fotographer96, I know you want a solution and all, but this is a volunteer forum. I only respond to the posts that I want to respond to. That typically means posts where the user has done a sufficient job of explaining the problem and providing the necessary info. And, to be honest, you actually did a better job than most. But, I too was turned off by the fact that you didn't provide any code and it looked like you just wanted someone to do it for you.

 

The ONLY people I would help via an unsolicited PM would be those that I have developed a relationship with. Sending a PM to someone you don't know, out of the blue, asking them to do something for you is no better than spam.

 

But, in the interest of being a forgiving person, I will give you a solution anyway. But, please do not take this as an opportunity to do the same in the future or I will make sure to never respond to your posts again.

 

Not tested, so there may be some syntax errors:

$query = "SELECT pp.ID, pp.Name, p.pageName, p.pageID
          FROM parent_pages AS pp
          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 .= "</ul>\n";
            $menuHTML .= "</li>\n";
        }
        $currentParentID = $row['ID'];
        //Open new parent LI, create link and open submenu UL
        $menuHTML .= "<li class=\"mainnav\">";
        $menuHTML .= "<a rel=\"nofollow\" href=\"view.php?id={$row['ID']}\">{$row['Name']}</a>";
        $menuHTML .= "<ul>\n";
    }

    //Create submenu LI
    $menuHTML .= "<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 .= "</ul>\n";
$menuHTML .= "</li>\n"
$menuHTML .= "</ul>\n";

echo $menuHTML;

Link to comment
Share on other sites

What you are asking for...

<ul>

      <li>parent_page1</li>

            <ul>

                  <li>sub-page1</li>

                  <li>sub-page2</li>

                  <li>sub-page3</li>

            </ul>

      </li>

 

...is invalid HTML!

 

You need to remove the closing </li> after the parent name. You already have the correct closing LI at the end of the list of submenus. The code I provided should work as you ask.

Link to comment
Share on other sites

What you are asking for...

<ul>

      <li>parent_page1</li>

            <ul>

                  <li>sub-page1</li>

                  <li>sub-page2</li>

                  <li>sub-page3</li>

            </ul>

      </li>

 

 

...is invalid HTML!

 

You need to remove the closing </li> after the parent name. You already have the correct closing LI at the end of the list of submenus. The code I provided should work as you ask.

 

Sorry, typing mistake

 

Also, for completeness, this isn't recursion.  This is simple while-loop iteration.  Recursion is a specific thing.

 

Yeah I know.. and there is a problem with that code too..

mjdamato's code gives me that result:

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

Link to comment
Share on other sites

Sorry, change this line

        if($currentParentID===false)

 

To this

        if($currentParentID!==false)

 

Also, for completeness, this isn't recursion.  This is simple while-loop iteration.  Recursion is a specific thing.

 

By the way, have you ever done a google search on the word "recursion"? Only a "geeky" company such as google would let someone implement what they did.

Link to comment
Share on other sites

Also, for completeness, this isn't recursion.  This is simple while-loop iteration.  Recursion is a specific thing.

 

By the way, have you ever done a google search on the word "recursion"? Only a "geeky" company such as google would let someone implement what they did.

 

Yeah, it's awesome. :)

Link to comment
Share on other sites

Sorry, change this line

        if($currentParentID===false)

 

To this

        if($currentParentID!==false)

 

result:

<ul>
</ul>
</li>
<li class="mainnav"><a rel="nofollow" href="view.php?id=1">parent_page</a><ul>

<li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=1">page</a></li>
<li class="mainnav"><a rel="nofollow" href="view.php?id=4">parent_page</a><ul>
<li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=3">page</a></li>
<li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=2">page</a></li>
</ul>
</li>
</ul>
		<ul>
                        <li class="mainnav"><a rel='nofollow' href='view.php?id=1'>parent_page</a>

                <ul>
                    <li class="subnav"><a rel='nofollow' class="sublink_nav" href='viewpage.php?pageid=1'>page</a></li>
                </ul>
            </li>
                        <li class="mainnav"><a rel='nofollow' href='view.php?id=4'>parent_page</a>
                <ul>
                    <li class="subnav"><a rel='nofollow' class="sublink_nav" href='viewpage.php?pageid=2'>page</a></li>

                </ul>
            </li>
                        <li class="mainnav"><a rel='nofollow' href='view.php?id=4'>parent_page</a>
                <ul>
                    <li class="subnav"><a rel='nofollow' class="sublink_nav" href='viewpage.php?pageid=3'>page</a></li>
                </ul>
            </li>
                                    <li class="mainnav"><a rel='nofollow' href='view.php?id=2'>parent_page</a></li>

                        <li class="mainnav"><a rel='nofollow' href='view.php?id=3'>parent_page</a></li>
                        </ul>

 

May it would help, if it is coded with recursion? I think so.. because it's hierarchical

Link to comment
Share on other sites

May it would help, if it is coded with recursion? I think so.. because it's hierarchical

If you only have two levels then, no, you don't want recursion.

 

Also, what you are posting is completely impossible based upon the code I provided. PLease post the code you have.

Link to comment
Share on other sites

May it would help, if it is coded with recursion? I think so.. because it's hierarchical

If you only have two levels then, no, you don't want recursion.

 

Also, what you are posting is completely impossible based upon the code I provided. PLease post the code you have.

 

<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">Seite 1</a></li>
<li class="mainnav"><a rel="nofollow" href="view.php?id=4">Wellness</a><ul>
<li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=3">Design</a></li>
<li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=2">SPA</a></li>
</ul>
</li>
</ul>
		<ul>
		<li class="mainnav"><a href="index.php">STre</a></li>
                        <li class="mainnav"><a rel='nofollow' href='view.php?id=1'>Star</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=2'>SPA</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'>Design</a></li>
                </ul>
            </li>
                                    <li class="mainnav"><a rel='nofollow' href='view.php?id=2'>Group</a></li>

                        <li class="mainnav"><a rel='nofollow' href='view.php?id=3'>tySpa</a></li>
                        </ul>
	</div>

 

I thought I would make it easier to read for you.. but here it is.

Link to comment
Share on other sites

Huh, I said post the code not the HTML markup. I see problems in the output you are posting that I don't see as possible from the code I gave you. I want to see the PHP code as you have implemented it.

 

here it is:

		<?php 
		$query = "SELECT pp.ID, pp.Name, p.pageName, p.pageID
          FROM parent_pages AS pp
          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 .= "</ul>\n";
		            $menuHTML .= "</li>\n";
		        }
		        $currentParentID = $row['ID'];
		        //Open new parent LI, create link and open submenu UL
		        $menuHTML .= "<li class=\"mainnav\">";
		        $menuHTML .= "<a rel=\"nofollow\" href=\"view.php?id={$row['ID']}\">{$row['Name']}</a>";
		        $menuHTML .= "<ul>\n";
		    }

		    //Create submenu LI
		    $menuHTML .= "<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 .= "</ul>\n";
		$menuHTML .= "</li>\n";
		$menuHTML .= "</ul>\n";

		echo $menuHTML;

	?>

Link to comment
Share on other sites

Please explain why should I help you when you modify the code I provide you which then leads to the code to fail? If you don't understand something - ask.

 

I specifically formatted the comparison operator (although I missed one) to prevent a potential problem which would occur if you had a parent ID of 0. But you do have a parent ID of 0, don't you? And you, in your infinite wisdom, decided that you knew better. Which resulted in the code not working.

 

Change the following two lines

 

This:

if($currentParentID != $row['ID'])

To this:

if($currentParentID !== $row['ID'])

 

 

AND This:

if($currentParentID==false)

To this:

if($currentParentID===false)

 

The problem is that when comparing values a 0 (zero) will compare to a bolean false if you don't use a strict comparison (as will a null value or empty string). By adding the additional equal sign you are comparing if the two values are equal AND of the same type.

 

http://php.net/manual/en/language.operators.comparison.php

Link to comment
Share on other sites

Please explain why should I help you when you modify the code I provide you which then leads to the code to fail? If you don't understand something - ask.

 

I specifically formatted the comparison operator (although I missed one) to prevent a potential problem which would occur if you had a parent ID of 0. But you do have a parent ID of 0, don't you? And you, in your infinite wisdom, decided that you knew better. Which resulted in the code not working.

 

Change the following two lines

 

This:

if($currentParentID != $row['ID'])

To this:

if($currentParentID !== $row['ID'])

 

 

AND This:

if($currentParentID==false)

To this:

if($currentParentID===false)

 

The problem is that when comparing values a 0 (zero) will compare to a bolean false if you don't use a strict comparison (as will a null value or empty string). By adding the additional equal sign you are comparing if the two values are equal AND of the same type.

 

http://php.net/manual/en/language.operators.comparison.php

 

Hey,

A feew posts befor you told me to change a line, that one I missed and did a mistake..

 

Yea I do. It was a mistake! I never thought I do better! I'm a beginner how could I?

 

Ok.

 

The effect doesn't change..

 

I have to go now, tomorrow we will continue talking.

Link to comment
Share on other sites

It is impossible to get the results you displayed unless the ID of the first parent record is the boolean false and I find that hard to believe.

 

You show this as the first three lines

<ul>
</ul>
</li>

 

Then ending UL and LI tags would be generated in the IF condition at the bottom of the code displayed below. But $currentParentID is set as false before the loop runs so that IF condition could never be true in the first instance of that loop.

 

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 .= "</ul>\n";
           $menuHTML .= "</li>\n";
        }

 

Output your tables and attach to the post.

Link to comment
Share on other sites

Database Structure:

uh23zkw2.jpg

 

Database Entries:

5lbhygmm.jpg

 

HTML Output:

		<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>
<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">Design</a></li>
<li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=2">tySpa</a></li>
</ul>
</li>
</ul>
		<ul>
		<li class="mainnav"><a href="index.php">Starline</a></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=2'>tySpa</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'>Design</a></li>
                </ul>
            </li>
                                    <li class="mainnav"><a rel='nofollow' href='view.php?id=2'>Group</a></li>

                        <li class="mainnav"><a rel='nofollow' href='view.php?id=3'>Spa</a></li>
                        </ul>

 

Hope it helps..

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.