Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
David

Ch 1 - Script 1.3 - Getting Recursive Function To Return Html

Recommended Posts

Modified the recursive function on page 19 to return divs instead of an ordered list.

 

Function:

    function make_list($parent, $sub=FALSE) {
        
        // set attribute
        if (!$sub) {
        
            $attribute = 'id="menu"';

        } else {
        
            $attribute = 'class="submenu"';
        }
        
        // set tasks access
        global $tasks;
       
 
        foreach ($parent as $task_id => $todo) {
       
            // new loop?
            if (!$sub) { echo '<div id="main">'; }
            
            // build div
            echo '<div ' . $attribute . '>' . $todo . '</div>';
     
            // check for next array
            if (isset($tasks[$task_id])) {
                
                // call function again
                make_list($tasks[$task_id], TRUE);
            
            } else {
            
                // close 'main' div
                echo '</div>';
            
            }
        }
    }

Output:

<div id="main">
    <div id="menu">Task 1</div>
    <div class="submenu">Subtask 1</div>
    <div class="submenu">SubSubTask 1</div>
</div>
<div id="main">
    <div id="menu">Task 2</div>
    <div class="submenu">Subtask 2</div>
    <div class="submenu">SubSubTask 2</div>
</div>
<div id="main">
    <div id="menu">Task 3</div>
    <div class="submenu">Subtask 3</div>
</div>
<div id="main">
    <div id="menu">Task 4</div>
    <div class="submenu">Subtask 4</div>
</div>

The problem is:

 

Both the SubTask and SubSubTask divs have the same class of 'submenu'.

    <div class="submenu">Subtask 1</div>
    <div class="submenu">SubSubTask 1</div>

I'm trying to get them to have different class names, like so:

    <div class="submenu">Subtask 1</div>
    <div class="subsubmenu">SubSubTask 1</div>

How might I structure the function to accomplish this?

 

Any ideas would be helpful.

 

 

 ~ David

Share this post


Link to post
Share on other sites

If I'm following correctly, you could add a counter to the loop so that when the counter is 0, it uses the submenu class and when it's not, it uses the subsubmenu class (incrementing the counter within the loop). 

That being said this is probably unnecessary as what you're trying to accomplish can probably be done using CSS instead of different class names.

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.

Loading...
Sign in to follow this  

×
×
  • Create New...