Project Challenge: Edit and Delete Tasks

Let's add the edit and delete features to the project.

Problem statement

In this chapter, we looked at editing and deleting data. Now, we will add these features to the task management application.

Tasks:

  • Create a pages/functions/task-crud.php file, and create the reusable functions normalize_submitted_data(), validate_normalized_data(), load_all_tasks_data(), save_all_tasks_data(). They will be very similar to the functions we have created in this chapter in tour-crud.php.
  • Include the functions in the bootstrap.php script so they are available on all pages. Use the new functions everywhere you can, and verify that everything still works.
  • Modify the /create-task page to set a unique 'id' key on new task data arrays.
  • Create an /edit-task page, and add a link from the /list-tasks page to the /edit-task page, providing the ID of the task as a query parameter.
  • The /edit-task page shows the same form as the /create-task page but is pre-filled with the data for the task that is being edited. When saving, the data also needs to be validated, and the changes need to be saved to the tasks.json file. To make loading and saving the data for one task by its ID easier, create load_task_data(int $id) and save_task_data(array $data) functions, similar to the load_tour_data(int $id) and save_tour_data(array $data) functions that we created in this chapter.
  • Make sure that you can’t edit the task of another user by comparing $task['username'] to $_SESSION['authentication_user'].
  • On the /list-tasks page, add a form next to each task with a hidden input field named id containing the task ID and a “Done” button. When you press the button, a POST request should be made to the /mark-as-done page.
  • Create the /mark-as-done page where you load the task data for the given ID (using load_task_data()). To mark the task as done, add a key to the task data array: $taskData['isDone'] = true. After doing this, save the task data (using save_task_data()), then redirect to the /list-tasks page.
  • Again, make sure that you can’t mark a task of another user as done by comparing $task['username'] to $_SESSION['authentication_user'].
  • Update the /list-tasks page to only show the tasks that haven’t been done yet.

Get hands-on with 1400+ tech skills courses.