Advertisement
dajare

Children helper

Feb 19th, 2010
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. @@Children.php Helper:@@
  2. <?php
  3. /**
  4.  * Creates an array with all pages, sub-pages and their respective levels.
  5.  * It can be used to format the list of pages differently in various views.
  6.  *
  7.  * @param string $parent_id - The id of the parent page.
  8.  * @param int $level - The level where to start.
  9.  * @return array  The array with all the page objects, with an extra 'level' attribute, informing the page's level.
  10. */
  11.     function getAllChildren($parent_id=0, $level=0) { // begin method
  12.         $children_array = array();
  13.         if(Page::hasChildren($parent_id)) {
  14.             $pages = Page::childrenOf($parent_id);
  15.             foreach($pages as $child) {
  16.                 $child->level = $level;
  17.                 $children_array[] = $child;
  18.                 $children_array = array_merge($children_array, getAllChildren($child->id, $level+1));
  19.             }
  20.         }
  21.         return $children_array;
  22.     } // End method
  23. ?>
  24.  
  25. @@Listing code as used on "Music" page:@@
  26.  
  27. <ul>
  28. <?php use_helper('Children');
  29.   $allKids = getAllChildren('40');
  30.   foreach ($allKids as $kid) :
  31. ?>
  32.   <li><?php echo $kid->title() .' ['. $kid->level().']'; ?></li>
  33. <?php endforeach; ?>
  34. </ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement