Jump to content
Larry Ullman's Book Forums

Recommended Posts

My question relates to the code for view_tasks2.php (copied below) on or about page 35 in larry's book.

 

About 3/4 of the way down the code you will see this snippet

 

// Complete the query and execute:
$q = substr($q, 0, -2) . ')';
$r = mysqli_query($dbc, $q);

 

 

I am wondering why he's decided to set the substr parameters to 0 and -2. I believe -2 in this case will mean that it finishes 2 from the end of the length of q, but why do that?

 

Thanks if you can help.

 

<!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', 'root', 'test', 8889) 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 looked at something similar the other day and had to take a step back to make sure I followed it right. From what I surmised, the $q isn't completed initially, it ends mid way through completion, the -2 does remove the last 2 charachters of $q in this case

' ;

then the foras loop is completed adding to the query and then the query is ended finally. If he didn't do that then either the $q would fail initially as it isn't terminated correctly initially I.e its missing the ' and the ; or after the foras loop the query would have 'insert query string here'; and then larry couldn't append extra to the query because it would already be completed from the previous step. This what I understood it to be, correct me if I'm wrong though ;)

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...