Jump to content
Larry Ullman's Book Forums

Search the Community

Showing results for tags 'recursion'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Single Editions
    • Modern Javascript: Develop and Design
    • The Yii Book
    • Effortless Flex 4 Development
    • Building a Web Site with Ajax: Visual QuickProject
    • Ruby: Visual QuickStart Guide
    • C++ Programming: Visual QuickStart Guide
    • C Programming: Visual QuickStart Guide
    • Adobe AIR: Visual QuickPro Guide
  • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
    • PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (1st Edition)
  • PHP for the Web: Visual QuickStart Guide
    • PHP for the Web: Visual QuickStart Guide (5th Edition)
    • PHP for the Web: Visual QuickStart Guide (4th Edition)
    • PHP for the Web: Visual QuickStart Guide (3rd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (2nd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (1st Edition)
  • Effortless E-commerce with PHP and MySQL
    • Effortless E-Commerce with PHP and MySQL (2nd Edition)
    • Effortless E-Commerce with PHP and MySQL
  • PHP Advanced: Visual QuickPro Guide
    • PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
    • PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
    • PHP Advanced: Visual QuickPro Guide
  • MySQL: Visual QuickStart Guide
    • MySQL: Visual QuickStart Guide (2nd Edition)
    • MySQL: Visual QuickStart Guide (1st Edition)
  • Other
    • Announcements
    • Newsletter, Blog, and Other Topics
    • Forum Issues
    • Social

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 1 result

  1. Hi, I am trying to use similar recursive logic to Larry's in chapter 1. I have modified my code to enable you to mark tasks as completed and that works just fine. Now I am trying to use the recursive approach to list out the completed tasks in the same manner as 'live' tasks get listed. I create a two-dimensional array of completed tasks and then try to use the recursive logic to list out those tasks. It works only partially in that, say, a completed task has a parent, then that prints out OK. But, the logic in the display_completed_tasks recursive function does not 'pop back up' to the next highest level after (correctly) printing out part of the array. Maybe it is because the key of the first array passed is not necessarily zero - you can see that I save the first key in the list_completed_tasks function. I have printed out the arrays and they look OK. Here's my code: function list_completed_tasks() { global $dbc, $completed_tasks; $first_time_through = TRUE; $qa = 'SELECT task_id, parent_id, task FROM tasks WHERE date_completed IS NOT NULL ORDER BY parent_id, date_added ASC'; $ra = mysqli_query($dbc, $qa); $completed_tasks = array(); // Initialize the storage array: while (list($task_id, $parent_id, $task) = mysqli_fetch_row($ra)) { // Add to the array: $completed_tasks[$parent_id][$task_id] = $task; if ($first_time_through) { $first_key = $parent_id; $first_time_through = FALSE; } } echo '<h3>Completed Tasks Array Details</h3><pre>' . print_r($completed_tasks, 1) . '</pre>'; // For debugging and general interest display_completed_tasks($completed_tasks[$first_key]); } // end list_completed_tasks function function display_completed_tasks($complete_tasks) { // note: the value passed is an array echo '<p>Coming into display_completed_tasks: <pre>' . print_r($complete_tasks, 1) . '</pre>'; global $completed_tasks; // Start an ordered list: echo '<ol>'; // Loop through each subarray: foreach ($complete_tasks as $task_id => $done) { // Display the item: echo <<<EOT <li><input type="checkbox" name="marked_tasks[$task_id]" value="$done" /> $done EOT; // Note that the above creates an array of task_ids, even if only one checkbox is selected // The array is later accessed through the super-global $_POST['marked_tasks'] // Check for subtasks: if (isset($completed_tasks[$task_id])) { // isset checks the variables from left to right // in this instance, the isset is looking to see if the current sub-array element's task_id // is a parent_id in the multi-dimensional array $completed_tasks // Call this function recursively: // echo '<p>About to call the function recursively - calling it with: <pre>' . print_r($completed_tasks[$task_id], 1) . '</pre>'; // For debugging and general interest display_completed_tasks($completed_tasks[$task_id]); // note that $completed_tasks[$task_id] is an array in its own right (a sub-array of $completed_tasks) } // Complete the list item: echo '</li>'; } // End of FOREACH loop. // Close the ordered list: echo '</ol>'; } // End of display_completed_tasks() function. Any advice will be appreciated and thank you in anticipation.
×
×
  • Create New...