Jump to content
Larry Ullman's Book Forums

Chapter 1, Make_List() Question


Recommended Posts

In chapter one, make_list() evolves into a function that declares $tasks as a static variable so the function can remember the $tasks array between function calls.

 

I modified make_list() in the following way and it appears to work fine, at least for a three deep nested list:

 

function make_list($parent, $tasks) {
    echo "<ol>\n";
    foreach ($parent as $task_id => $task) {
        echo <<<EOT
        <li><input type="checkbox" name="tasks[$task_id]" value="done"> $task
EOT;
        # Check for subtasks:
        if (isset($tasks[$task_id])) {
            make_list($tasks[$task_id], $tasks);
        }

        echo "</li>\n"; // Complete the list item.
        
    } // End the foreach loop.    
    echo "</ol>\n"; // Close the ordered list.
    
} //------------------------------------------------------------>end make_list()

 

The first function call remains the same; i.e., if (!empty($tasks)) make_list($tasks[0], $tasks);

 

Logically, am I missing something here? I've only tested it so far with PHP 5.2.

 

Thank you,

Hacker

Link to comment
Share on other sites

Looks fine to me. You'll have a performance hit constantly passing a huge array around, though. Say the array requires 4KB of memory. Each function call requires 4KB of memory (plus the other stuff). The problem is that with recursion, the first function call is not completed until the very end, like so:

 

Call function #1

Call function #2

Call function #3

...

Call function #N

Complete function #N

...

Complete function #3

Complete function #2

Complete function #1

 

So all of that memory required for every function call is retained until the Nth recursive call completes and all the calls complete backwards. This can be bad.

Link to comment
Share on other sites

BTW Larry,

 

How would one output the parent_id to the HTML list so one can easily reference the parents to their subtasks directly in the <li>?

 

Thank you,

Hacker

 

Here's what I have so far:

 

function make_list($parent, $x=NULL) {
    static $tasks;

    if (is_array($x)) {
        $tasks = $x;
    } else {
        $parent_id = $x;
    }

    
    echo "<ol>\n"; // Start the ordered list
    foreach ($parent as $task_id => $task) {
        if ($parent_id > 0) {
            echo <<<EOT
<li><input type="checkbox" name="tasks[$task_id]" value="done"> Task Id $task_id: $task <span style="color:red;">(of parent task id $parent_id)</span>
EOT;
        } else {
            echo <<<EOT
<li><input type="checkbox" name="tasks[$task_id]" value="done"> Task Id $task_id: $task <span style="color:red;">(no parent task)</span>
EOT;
        }

        # Check to see if the last task printed is a parent task:
        if (isset($tasks[$task_id])) {
            make_list($tasks[$task_id], $task_id);
        
        echo "</li>\n"; // Complete the list item.
        
    } // End of foreach loop.    
    echo "</ol>\n"; // Close the ordered list.
} //------------------------------------------------------------>end make_list()

Link to comment
Share on other sites

 Share

×
×
  • Create New...