SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Recursive functions and jumping back

    Hi All,

    I'm really stuck with this one - although it's probably the world's most obvious answer!

    I'm updating someone else's code and have the below function - it's used to display menu items from a CMS. It works fine in PHP 5.2, but in 5.1 only shows the first top level nav item, and the first second level item.

    PHP Code:
    function sortMenu(&$menus$parent) {
        foreach (
    $menus as $menu) {
            if (
    $menu['parent'] == $parent) {
                
    $sortedMenu[$menu['id']] = $menu;
                if (
    $temp sortMenu($menus$menu['id'])) {
                        
    $sortedMenu[$menu['id']]['child'] = $temp;
                }
            }
        }

    It's because it is going down through the recursive functions, but is not starting where it left off after a recursion (if you see what I mean!)

    Any ideas what I'm doing wrong?

    Cheers,
    Kev

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can see immediately that your function does not return anything, which is why you can't assign it to a variable like you're doing with 'if ($temp = sortMenu($menus, $menu['id'])) {
    '

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Did you turn error reporting on, to print onto the page? If so were there any warnings?

  4. #4
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry - I removed the return statement when I was trimming from brevity - The final part is:

    return $sortedMenu;

  5. #5
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    error_reporting(E_ALL) returns no errors/notices at all...

    I have added some debug output which shows me that when the parent IDs match up it starts the second recursive function, but never moves back up the loop...

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Beats me, why would that not work on 5.1?

    I apologise for making a stab in the dark, but how about if you removed the & in "&$menus" ? Without treating the array as a reference to itself it'd be marginally slower (as it would then be working on a copy) - but even so I still cannot see how this behaviour differs between .1 and .2.

    Is this function a method inside a class?

  7. #7
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Removing the & did the trick - although I'm sure I tried it previously!

    Not sure if the differences in PHP version was unrelated...

    Thank you all for your help - much appreciated!

    Cheers,
    Kev


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •