0

I'm writing a nav menu and got stuck.

Basically I have a foreach loop that runs some logic if a page id is 0. This logic runs another foreach loop that checks whether there are any subpages to the current page.If so, it define a third foreach loop to check whether there are any subsubpages to the current subpage.

The problem I'm having is that, I need the second and third foreach loops to loop back around and around to check for sub-sub-sub-pages and so on. So I have written the variables in the third foreach loop to be the same as the first foreach loop with the intention of putting the second and third foreach loops into a function that is then inserted into the function buildMenu(); where needed and that then calls itself later inside the function so that it loops over and over.

However it seems that by running through the third foreach loop the value for $b = $childpage['id']; changes so that when the second foreach loop loops back around and tries to see whether $c == the $b from the first foreach loop it no longer does because the value has been changed by the third loop.

So my question is can I maintain the value of $b in the second foreach loop so that when the second loop loops round on itself $b is still what was set by the first foreach loop but then also change the value of $b in the third loop so that the new value is used lower down the looping cycle.

Hopefully that makes sense but the code is below so hopefully you can see what I mean.

If it's not possible and there's a better way I'd be very grateful for it! Thanks

<?php  
      function buildMenu2($allpages){

        echo "<ul>\n";


 foreach($allpages as  $pageitem) {

     $a = $pageitem['parentid'];
     $b = $pageitem['id'];



     if($a == 0){

          echo "<li><a href=" . $pageitem['url'] . ">" . $pageitem['linklabel'] . "</a>";



          echo "<ul>\n";
            foreach($allpages as  $childpage) {

                $c = $childpage['parentid'];
                $d = $childpage['id'];

                if ($c == $b){
                    echo "<li><a href=" . $childpage['url'] . ">" . $childpage['linklabel'] . "</a>";


                        echo "<ul>\n";
                            foreach ($allpages as $childpage) {
                                $a = $childpage['parentid'];
                                $b = $childpage['id'];

                                if ($a == $d) {
                                    echo "<li><a href=" . $childpage['url'] . ">" . $childpage['linklabel'] . "</a>";
                                    echo "</li>";
                                    };
                                };

                     echo "</ul>\n";    


                    echo "</li>";
                            };
                        };

            echo "</ul>\n"; 






          echo "</li></br><br/><br/>";


         };


  };  

    echo "</ul>\n";

      };   
   ?>
Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
Smokescreen
  • 189
  • 3
  • 4
  • 13
  • Have you heard about recursive functions? – Prince Agrawal Mar 14 '14 at 12:15
  • Vaguely but I'm not that familiar - I think I may have solved the problem above but may have caused another to do with functions which may be where recursive functiosn could come in. It seems that if I change the $b in the third foreach to $e and then call an if statement with an or clause in the second foreach statement e.g. if (($c == $b) or ($c == $e)) I get the result I want but when i try to put it all into a repeating function it breaks - any ideas? – Smokescreen Mar 14 '14 at 12:18

1 Answers1

0

Why can't you create a function & call it recursively to get subpages of a page?

I don't know much about php, but will give you pseudo code.. Hope it helps you

myRecursiveFunction(page)
{
   if (page.subpage!=nil) // checks if subpages are there
    {
        for each subpage in page.subpage
        {
            myRecursiveFunction(subpage);
        }
    }
  else{
      //write your logic here.. may be to get URL of the page
      }
}

Let me know if more info needed… :)

Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
  • thanks - I don't really know much about recursive functions but I shall go and research and see whether that will solve my issue :) – Smokescreen Mar 14 '14 at 12:26
  • @Smokescreen.. Let me know if it doesn't help you. If it helps, feel free to accept/upvote the answer.. :) – Prince Agrawal Mar 15 '14 at 10:59
  • Hi I've been trying to get my head around recursive functions but still don't quite seem to have cracked it yet and I was just wondering whether you could have a look at my latest try and see where I may be going wrong? Its in a different post as I was having trouble with something else and have edited that question to include my current situation - http://stackoverflow.com/questions/22465112/check-if-value-from-a-foreach-loop-is-in-an-array thank you for your help – Smokescreen Mar 18 '14 at 16:24