Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi. I keep getting 1 Notice and 1 Error after I delete all the tasks from the view_tasks2.php in chapter 1 Advanced PHP Object Oriented Programming.

Here is the output after I delete all the tasks:



Current To-Do List

The task(s) have been marked as completed!

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).

  1. Notice: Undefined offset: 0 in C:\xampp\htdocs\aqp\chp1\view_tasks2.php on line 83
  2. Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\aqp\chp1\view_tasks2.php on line 23


Can someone help me with the errors?


here is my code for the view_tasks.php file. It is the exact same code that is provided from the book source code minus my database username, password settings. I have labeled lines 83 and 23 so you can find them easier.


<!doctype html>
<html lang="en">
    <meta charset="utf-8">
    <title>View Tasks</title>
    <link rel="stylesheet" href="style.css">
<h2>Current To-Do List</h2>
<?php # Script 1.6 - 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) {
    global $tasks;
    echo '<ol>'; // Start an ordered list.
    foreach ($parent as $task_id => $todo) {              //line 23
        // Start with a checkbox!
        echo <<<EOT
<li><input type="checkbox" name="tasks[$task_id]" value="done"> $todo
        // Check for subtasks:
        if (isset($tasks[$task_id])) { 
        echo '</li>'; // Complete the list item.    
    } // End of FOREACH loop.
    echo '</ol>'; // Close the ordered list.
} // End of make_list() function.
// Connect to the database:
$dbc = mysqli_connect('localhost', 'root', 'password', 'my_db');
// Check if the form has been submitted:
    && isset($_POST['tasks']) 
    && is_array($_POST['tasks'])
    && !empty($_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);
$tasks = array();
while (list($task_id, $parent_id, $task) = mysqli_fetch_array($r, MYSQLI_NUM)) {
    $tasks[$parent_id][$task_id] =  $task;
// Make a form:
echo <<<EOT
<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">
make_list($tasks[0]);              //line 83
// Complete the form:
echo <<<EOT
<input name="submit" type="submit" value="Update" />
Link to comment
Share on other sites

Hi and welcome to the forum.


Have you checked that there are still some tasks in the database that are not marked as completed?  It seems possible that your database select is not returning any rows.  You can check this via testing the row-count immediately after you have run the select query.  If the row-count is zero (I usually check for less than 1) you can print a message saying that there are no 'live' tasks in the database.  If the row-count is greater than zero, then go ahead with the population of the multi-dimensional array.



$row_count = mysqli_num_rows($r);


This 'tasks' exercise is one of my favourite learning exercises and I have found that you can extend it in many ways.


Feel free to post more queries in this forum - a great community supports it.


Cheers, Necuima. 

Link to comment
Share on other sites

I am going to see if I can fix the bug. 

Has anyone else had this problem? It works fine while there are tasks to be completed but once all the tasks are deleted then I get this error. And the code is copied and pasted exactly from the exercise script.

Or if anyone has completed Chapter 1 successfully could you please send me your code for view_tasks.php

Link to comment
Share on other sites

Hi, glad to see that you have figured out how to solve the issue.


I'm assuming that you did a test for the number of rows returned from the "SELECT" and if there were no 'live' tasks in the database, did not try to populate the multi-dimensional array.


The errors you received were because the array was empty.



Link to comment
Share on other sites


  • Create New...