Project Challenge: Edit and Delete Tasks
Let's add the edit and delete features to the project.
We'll cover the following
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 functionsnormalize_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 intour-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 thetasks.json
file. To make loading and saving the data for one task by its ID easier, createload_task_data(int $id)
andsave_task_data(array $data)
functions, similar to theload_tour_data(int $id)
andsave_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 namedid
containing the task ID and a “Done” button. When you press the button, aPOST
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 (usingload_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 (usingsave_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.