SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    Sweden
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with printing array menu tree in a special way

    I have a tree menu based on arrays that I need help with. I've uploaded my menu test version at the following address: http://websitefactory.se/menu.php so you can see how it looks.

    The code to the test page for the menu: http://pastebin.com/mafcee87

    Now on to my problem!

    My problem:
    Right now the menu would appear under the following principle:

    Code:
    Page 1
    Page 2
         - Page 2.1
         - Page 2.2
              - Page 2.2.1
              - Page 2.2.2
         - Page 2.3
    Page 3
    Page 4
    But I want it to print out under this principle:

    Code:
              - Page 2.2.1
              - Page 2.2.2
         - Page 2
         - Page 2.1
         - Page 2.3
    Page 1
    Page 3
    Page 4
    Explanation: The further down the hierarchy come the higher the top of the page being built. The menu is built up instead of down. So the hierarchy level you are on will always be at the top. But within each level the links/childs will order as specified in the array. Please note that the link for the level you are at the moment (above Page 2.2) should not appear at all in the menu, and that the parent for each level is to be with the same level at the top (above items Page 2).

    It would mean much for me to make it work. So please take a look and try help me! Thank you!
    Last edited by Peter Westerlund; May 7, 2009 at 10:32.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    Sweden
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone? Please

  3. #3
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your suggested menu layout makes no sense / isn't logical - why is 2.1 & 2.3 at the same level as 2? You need to better define how you want the menu output I think, I'm struggling to understand what you are after.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    Sweden
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It may seem illogical, but I still think it is quite logical. What it does is that it shows the most relevant menu links to the page as high up on the page as possible, the first your eyes read. 2 should be located as a link title above the 2.1 and 2.3, at the same level. I don't think it's illogical. However, the question is if it's possible to do?

  5. #5
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why is 2 at the same level as its children, but no others are like this? You need to clearly define the 'rules' for how it is laid out first (unambiguously), because I still don't really understand your thinking. It will almost certainly be possible, you need to define what you want a bit better though. Saying 'put the most relevant links as high up as possible' is very vague - how do you determine what is relevant? How high up should they go?

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    Sweden
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since the top level has no parent, no parent can be a link-header above those top links. The next level has a parent from the top level. And it's not the level that the page we are visiting is on, therefore, the level's parent shall be printed above their children. When printed above their children, it should not be printed at the level it actually belongs to.

  7. #7
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, and what happens if you have another level? Can you give an example of what it should look like on page 2.2.1.1 for example? Just so I have a clearer picture

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    Sweden
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     
              - Page 2.2
              - Page 2.2.1
              - Page 2.2.2
         - Page 2
         - Page 2.1
         - Page 2.3
    Page 1
    Page 3
    Page 4
    The only thing that happens is that the 2.2 link now shows in the menu. This is because we are not in that level anymore. If 2.2.1.1 is the only link on their own level, nothing is printed out for that level. Do you understand the principle now?

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    What would the HTML look like?

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I kind of understand what you're trying to do, but I really don't see any benefit of doing it at all. In fact, it will be pretty nasty to navigate. Usability-wise, it isn't a good idea.

    Surely simply highlighting the current page or something would make more sense?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    Sweden
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the context that the menu will be used in it will be very logical and practical. I will also use up arrows and mark clearly who are parents. I don't want it to be a discussion if the structure is logical or not. I just want to know how to make this possible.

  12. #12
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    My point is that if people who work with structure and logic every day think it sounds needlessly complicated, ordinary users will think so even more.

    Anyway, others have asked questions and unless you answer them, none of us can help you.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  13. #13
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a starting point for ya.
    PHP Code:
    <?php
    $myarr 
    = array(
        
    '1' => array(
            
    '1.1' => array(
                
    '1.1.1'
                
    ,'1.1.2'
            
    )
            ,
    '1.2' => array(
                
    '1.2.1'
                
    ,'1.2.2'
            
    )
        )
        ,
    '2' => array(
            
    '2.1' => array(
                
    '2.1.1'
            
    )
        )
        ,
    '3' => array(
            
    '3.1' => array(
                
    '3.1.1'
            
    )
        )
    );

    $curpage '2';
    $curpage_arr $myarr[$curpage];
    unset(
    $myarr[$curpage]);

    echo 
    "<pre>";
    print_r($curpage_arr);
    print_r(array_reverse($myarrTRUE));
    echo 
    "</pre>";
    There. I've done about half the work for you by showing you array_reverse(). The part about $curpage is half-baked, as I havent wrote the function to recurse the array searching for the the array key you provide as the $curpage. I think you can finish the rest (its not that difficult). Also, this structure does not make much sense to me logically. If I was navigating your site I'd probably leave. Have you considered an alternate method of navigation? Perhaps you use a drilldown navigation instead of a tree (you have a main menu and a sub menu, depending on the section you are in)?

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    Sweden
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I've tried a lot of possible variations. I feel that I soon reached the target. Now the code for the function to build the menu looks like this:

    PHP Code:
    <? function tree_menu($array,$sel)
    {
        
    $find $sel;
            foreach (
    $array as $code => $info)
            {
                if (
    findKeyRecursive($find$info))
                    
    $sele $code;
            }
        
        foreach (
    $array as $key => $value)
        {
            if (
    is_array($value['submenu']))
            {
            
    $level[$key] = array('name' => $value['name'], 'href' => $value['href'], 'submenu' => $value['submenu']);
                if (
    $sele == $key or $sel == $key)
                {
                
    tree_menu($value['submenu'],$sel);
                }
            }
            else
            {
            
    $level[$key] = array('name' => $value['name'], 'href' => $value['href']);
            }
        }
        
        echo 
    '<ul>';
        foreach (
    $level as $key => $value)
        {
            if (
    $sel !== $key)
            {
            echo 
    '<li><a href="'.$value['href'].'">'.$value['name'].'</a></li>';
            }
        }
        echo 
    '</ul>';
    ?>
    And when you are on Page 2.2.1, it looks like this:

    http://websitefactory.se/menu.php?sel=page221

    The only problem left is to get the parent to appear above their children rather than to lie in its corresponding level. Anybody know how to do it?

  15. #15
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why are you outputting the menu as 4 separate lists? Shouldn't they be nested lists?

  16. #16
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    Sweden
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I acually want them to be separated. I will show the result when I got it to work

  17. #17
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, but that doesn't describe the structure very well with markup - wouldn't it be better to mark them up as nested and then use CSS to separate them visually?

  18. #18
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    Sweden
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, that will work as well. But I still want the parents to be above their childs.

  19. #19
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Location
    Sweden
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone? please


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
  •