Welcome Guest, Not a member yet? Register   Sign In
Programming Challenge
#8

[eluser]seanloving[/eluser]
@sophistry - YAML might be the right way to prepare the $regions array

@helmutbjorg - I think you are right about the need for a recursive function

@BrianDHall - I think I'm doing something like you are suggesting - with strstr()

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

I ALMOST got this working... can somebody PLEASE help?

Once again, here's my desired output...
Code:
<div id="box">
     <div id="left">
          <div id="top"></div>
          <div id="bottom"></div>
     </div>
     <div id="right"></div>
</div>

But here is the actual output of my code
Code:
<div id="box">
     <div id="left">
          <div id="top"></div>
          <div id="bottom"></div>
          <div id="right"></div>
     </div>
</div>


And here is my code:
Code:
// pass this function an array of view partials (a.k.a. regions,  a.k.a. strings)
    // use this function to hierarchically structure (with div tags) the $regions
    function structure( &$regions=array() )
    {
        foreach( $regions as $parent=>$content )
        {
            // opening wrapper for this region
            echo "<div id=\"$parent\"><b>$parent</b> OPEN";
            
            // remove this region now that it has been rendered
            unset( $regions[$parent] );

            // see if this region is a parent of any remaining regions
            foreach($regions as $child=>$data)
            {
                if( ($parent & strstr($child,$parent)) )
                {
                    // at least one child was found so recurse
                    $regions = $this->structure( $regions );  
                    echo "<b>$parent</b> CLOSED</div>";
                    return $regions;
                }
            }
            echo "<br><b>".$parent."</b> CLOSED</div>";
        }
        
    }

I call that function like this:
Code:
// This is a test array. Remove it.
            $regions = array( 'box'             => 'the main box',
                              'box_left'        => 'the box on the left inside the main box',
                              'box_left_top'    => 'the box on the top inside the left box',
                              'box_left_bottom' => 'the box on the bottom inside the left box',
                              'box_right'       => 'the box on the left inside the main box'
                            );

            $regions = $this->structure( $regions );

Anyone? Please!! Thanks!

--Sean Loving


Messages In This Thread
Programming Challenge - by El Forum - 12-09-2009, 08:52 AM
Programming Challenge - by El Forum - 12-09-2009, 09:38 AM
Programming Challenge - by El Forum - 12-09-2009, 09:40 AM
Programming Challenge - by El Forum - 12-09-2009, 10:10 AM
Programming Challenge - by El Forum - 12-09-2009, 02:43 PM
Programming Challenge - by El Forum - 12-09-2009, 03:06 PM
Programming Challenge - by El Forum - 12-09-2009, 03:41 PM
Programming Challenge - by El Forum - 12-10-2009, 03:15 PM
Programming Challenge - by El Forum - 12-10-2009, 07:15 PM
Programming Challenge - by El Forum - 12-10-2009, 10:15 PM
Programming Challenge - by El Forum - 12-11-2009, 07:25 AM
Programming Challenge - by El Forum - 12-13-2009, 02:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB