Jump to content
John W.

Using recursion to find a parent? Suggestions orther than recursion?

Recommended Posts

Working on a project where I have different categories of Departments, under departments I have Offices, and each office has multiple pages.

DEPARTMENTS

     HUMAN RESOURCES

          About Human Resources
          Job Listings
               job 1 details page
               job 2 details page

     ADMINISTRATION

         About Administration
         Meet the Administrator

-----------------------------

stored in HUMAN RESOURCES is a background image and title of the department.

Instead of creating a background image for each office page (and it's sub pages like job 1 details page), I'm using the background image stored in HUMAN RESOURCES for all the child pages.

I'm familiar with using $page->rootParent, however, this will return DEPARTMENTS rather than the office, e.g. HUMAN RESOURCES that each sub page belongs to.

I thought about using a recursive function to traverse from the current page, when it reaches the template "office". Basically, if I'm in job 1 details page, it would traverse back up the tree until it reaches "office" template, such as "HUMAN RESOURCES".

Has anyone done something similar, but, using a built in method that I'm missing?

Thanks.

 

 

 

 

Share this post


Link to post
Share on other sites

Suggestions:

You could identify the HUMAN RESOURCES page as the first child of the root page:

$page->rootParent->children()->first()

Or: Add the background image to the rootPage and refer to it from the Office pages.

Share this post


Link to post
Share on other sites

Yeah, tried that, it pulls the first child under DEPARTMENTS

This doesn't work when you have several departments such as

DEPARTMENTS

     HUMAN RESOURCES

            About HR

            Job Listings

                      Job 1

                      Job 2

     ADMINISTRATION

            About Administration

    EMERGENCY OPERATIONS

            About Emergency Operations

 

In the case above, your suggestion would always return "Human Resources"

Share this post


Link to post
Share on other sites

I think I found a quick solution:

# cycles through all the current $page parents then
# returns the parent that is #x in the list
# $page is the current page, $parentNumber is the 
# parent to find and return in the tree

function getParent($page, $parentNumber) {
   $cnt=0;

   foreach($page->parents() as $p) {
    $cnt++;
     if($cnt == $parentNumber) {
          return $p;
      }
     }
     return false;
}

 #pass the current $page and return its third parent

 $parentPage = getParent($page,3);
 echo $parentPage->title;

 

Share this post


Link to post
Share on other sites

Couldn't you use this, assuming all offices, eg HUMAN RESOURCES have a template names "office"

$page->parents('template=office');

Actually you might want "parent" instead of "parents" to make it easier in this case.

  • Like 4

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...