Jump to content
Larry Ullman's Book Forums

The Recursion On Page 18


Recommended Posts

I'm really struggling with the recursion function starting on page 18. Eventually I will get if figured out as I always do.

But it is taking longer than I'd like.

So I'd like to ask if I can skip this part for now and come back to it later because I'd really like to explore the rest of the book. Or must I absolutely understand this before moving on?

 

I'd love to hear from someone who's finished the book

Link to comment
Share on other sites

I don't have the book, so sadly, I cannot help, but could you please post the recursive function that you're talking about?

 

Also, you might want to Google some tutorials on recursion to aid in your understanding. Sorry I can't provide a direct answer to your question.

Link to comment
Share on other sites

Thanks for the reply. Here is the script

I understand the table structure and the sql, I also understand all the multidimensional array

scripts preceding this.

If you ask me what don't I understand, I probably couldn't tell you.

I did the google / tutorial thing.

They always say these are "simple" and "basic".

Those "tutorials" really shouldn't say that because then, if you can't figure it out, you feel like a total DA.

-rant over

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>View Tasks</title>
</head>
<body>
<h3>Current To-Do List</h3>
<?php # Script 1.5 - view_tasks2.php
/* This page shows all existing tasks.
* A recursive function is used to show the
* tasks as nested lists, as applicable.
* Tasks can now be marked as completed.
*/
// Function for displaying a list.
// Receives one argument: an array.
function make_list ($parent) {
// Need the main $tasks array:
global $tasks;
// Start an ordered list:
echo '<ol>';

// Loop through each subarray:
foreach ($parent as $task_id => $todo) {

 // Display the item:
 // Start with a checkbox!
 echo <<<EOT
<li><input type="checkbox" name="tasks[$task_id]" value="done" /> $todo
EOT;

 // Check for subtasks:
 if (isset($tasks[$task_id])) {

  // Call this function:
  make_list($tasks[$task_id]);

 }

 // Complete the list item:
 echo '</li>';

} // End of FOREACH loop.

// Close the ordered list:
echo '</ol>';
} // End of make_list() function.
// Connect to the database:
$dbc = @mysqli_connect ('localhost', 'root', '', 'test') OR die ('<p>Could not connect to the database!</p></body></html>');
// Check if the form has been submitted:
if (isset($_POST['submitted']) && isset($_POST['tasks']) && is_array($_POST['tasks'])) {
// Define the query:
$q = 'UPDATE tasks SET date_completed=NOW() WHERE task_id IN (';

// Add each task ID:
foreach ($_POST['tasks'] as $task_id => $v) {
 $q .= $task_id . ', ';
}

// Complete the query and execute:
$q = substr($q, 0, -2) . ')';
$r = mysqli_query($dbc, $q);
// Report on the results:
if (mysqli_affected_rows($dbc) == count($_POST['tasks'])) {
 echo '<p>The task(s) have been marked as completed!</p>';
} else {
 echo '<p>Not all tasks could be marked as completed!</p>';
}
} // End of submission IF.
// Retrieve all the uncompleted tasks:
$q = 'SELECT task_id, parent_id, task FROM tasks WHERE date_completed="0000-00-00 00:00:00" ORDER BY parent_id, date_added ASC';
$r = mysqli_query($dbc, $q);
// Initialize the storage array:
$tasks = array();
while (list($task_id, $parent_id, $task) = mysqli_fetch_array($r, MYSQLI_NUM)) {
// Add to the array:
$tasks[$parent_id][$task_id] =  $task;
}
// For debugging:
//echo '<pre>' . print_r($tasks,1) . '</pre>';
// Make a form:
echo '<p>Check the box next to a task and click "Update" to mark a task as completed (it, and any subtasks, will no longer appear in this list).</p>
<form action="view_tasks2.php" method="post">
';
// Send the first array element
// to the make_list() function:
make_list($tasks[0]);
// Complete the form:
echo '<input name="submitted" type="hidden" value="true" />
<input name="submit" type="submit" value="Update" />
</form>
';
?>
</body>
</html>

Link to comment
Share on other sites

I know what you mean by understanding 99% of what's going on, but there's that 1% that's confusing you, and you can't really explain what it is. Well, you may have already read this article, but I found a good one on recursion in PHP, and the author doesn't patronize at all:

 

http://devzone.zend.com/article/1235

 

Anyway, I could explain the recursion part of the above function, but I don't want to explain a bunch of things you already know. Plus, while I don't have the book, I imagine that Larry already explained things pretty well.

 

I guess what I want to ask is the following: Does the concept of recursion make sense to you? I mean, why you call a function within itself. Does that make sense?

 

I think the tricky thing about recursion is that it's very similar to for and while loops, and with that said, loops seem a lot more intuitive, so the question becomes, why use recursion at all? Anyway, there are cases where recursion is easier than using a loop.

 

After reading the above article, if you're still confused, please let me know.

Link to comment
Share on other sites

Thanks Hart,

I will read the article and see if it helps. I do understand recursion and functions calling themselves. I have a couple more sources to check out too.

So, let me ask you a question: Can anything that can be done with a recursion be done with a loop or are there circumstances where one must use a recursion and nothing else will work?

Link to comment
Share on other sites

 Share

×
×
  • Create New...