SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I extend the conditional statement?

    Currently, I have two functions:
    PHP Code:
    function page_indent_all($menu$pid 0) {
      foreach (
    $menu as $nid => $node) {
        if (
    $node['pid'] == $pid) {
          
    $output[$nid] = str_repeat('   '$node['level']) . $node['title'];            
          
    page_indent_all($menu$nid);
        }
      }    
      return 
    $output;
    }

    function 
    page_indent_no_children($pages$active_nid$nid 0) {
      foreach (
    $pages as $nid => $page) {
        if ((
    $page['pid'] == $pid) && ($page['pid'] != $active_nid) && ($page['nid'] != $active_nid)) {
          
    $output[$nid] = str_repeat('   '$page['level']) . $page['title'];
          
    page_indent_no_children($pages$active_nid$nid);
        }
      }
      echo 
    $output;
      return 
    $output;

    These functions are identical, except the if-condition statement in the second one is extended from the first. I now wanted to rewrite them into one function, with a new parameter $children, something like:

    PHP Code:
    function page_indent($pages$active_nid$nid 0$children false) {
      foreach (
    $pages as $nid => $page) {
        
    $condition = ($page['pid'] == $pid);
        if (
    $children) {
          
    $condition.= (&& ($page['pid'] != $active_nid) && ($page['nid'] != $active_nid));
        }
        if (
    $condition) {
          
    $output[$nid] = str_repeat('   '$page['level']) . $page['title'];
          
    page_indent($pages$active_nid$nid$children);
        }
      }

    This obviously doesn't work, but I don't know how I should rewrite it so it does work. How can I extend the if-condition statement if children is set to true?

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You don't want to contantinate there. Just keep comparing.
    PHP Code:
        if ($children) { 
          
    $condition $condition && ( $page['pid'] != $active_nid && $page['nid'] != $active_nid ); 
        } 
    - Robert


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
  •