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

[eluser]seanloving[/eluser]
How do I write an algorithm that takes an array like this...
Code:
$regions = array ( 'box',
                   'box_left',
                   'box_left_top',
                   'box_left_bottom',
                   'box_right');

and generates HTML like this...
Code:
<div id="box">
     <div id="left">
          <div id="top"></div>
          <div id="bottom"></div>
     </div>
     <div id="right"></div>
</div>
?

Need the algorithm to understand the (hopefully obvious) hierarchical naming convention. Here's what I have so far
Code:
foreach( $regions as $region )
    {
        // first get the unique region name
        // the unique region name is the last word in the string
        $id = strrchr($region, '_');
        if( !$id )
        {
            // handle the first element
            $id=$region;
        }
        else
        {
            // get rid of the leading _
            $id = substr($id,1);
        }
        echo "<div id=\"$id\">$id";
        echo "</div>";
    }
Can anybody help me figure out the hierarchy part? Thanks in advance.

--Sean Loving


Thanks


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