Search the Community
Showing results for tags 'recursion'.
-
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.