I have the following code:
<?php
//get tasklist array from POST
$task_list = filter_input(INPUT_POST, 'tasklist', FILTER_DEFAULT,
FILTER_REQUIRE_ARRAY);
if ($task_list === NULL) {
$task_list = array();
$task_list[] = 'Write chapter';
$task_list[] = 'Edit chapter';
$task_list[] = 'Proofread chapter';
}
//get action variable from POST
$action = filter_input(INPUT_POST, 'action');
//initialize error messages array
$errors = array();
//process
switch( $action ) {
case 'Add Task':
$new_task = filter_input(INPUT_POST, 'newtask');
if (empty($new_task)) {
$errors[] = 'The new task cannot be empty.';
} else {
// $task_list[] = $new_task;
array_push($task_list, $new_task);
}
break;
case 'Delete Task':
$task_index = filter_input(INPUT_POST, 'taskid', FILTER_VALIDATE_INT);
if ($task_index === NULL || $task_index === FALSE) {
$errors[] = 'The task cannot be deleted.';
} else {
unset($task_list[$task_index]);
$task_list = array_values($task_list);
}
break;
case 'Delete All Task' :
if (empty($task_list)) {
$errors[] = 'The list is already empty.';
} else {
foreach ($task_list as $key => $value) {
unset($task_list[$key]);
}
$task_list = array_values($task_list);
}
break;
case 'Modify Task':
$task_index = filter_input(INPUT_POST, 'taskid', FILTER_VALIDATE_INT);
if ($task_index === NULL || $task_index === FALSE) {
$errors[] = 'The task cannot be modified.';
} else {
$task_to_modify = $task_list[$task_index];
}
break;
case 'Save Changes':
$i = filter_input(INPUT_POST, 'modifiedtaskid', FILTER_VALIDATE_INT);
$modified_task = filter_input(INPUT_POST, 'modifiedtask');
if (empty($modified_task)) {
$errors[] = 'The modified task cannot be empty.';
} else if($i === NULL || $i === FALSE) {
$errors[] = 'The task cannot be modified.';
} else {
$task_list[$i] = $modified_task;
$modified_task = '';
}
break;
case 'Cancel Changes':
$modified_task = '';
break;
case 'Promote Task':
$task_index = filter_input(INPUT_POST, 'taskid', FILTER_VALIDATE_INT);
if ($task_index === NULL || $task_index === FALSE) {
$errors[] = 'The task cannot be promoted.';
} else if ($task_index == 0) {
$errors[] = 'You can\'t promote the first task.';
} else {
// get the values for the two indexes
$task_value = $task_list[$task_index];
$prior_task_value = $task_list[$task_index-1];
// swap the values
$task_list[$task_index-1] = $task_value;
$task_list[$task_index] = $prior_task_value;
break;
}
case 'Sort Tasks':
sort($task_list);
break;
}
include('task_list.php');
?>
The default values list as:
Write chapter Edit chapter Proofread chapter
When I try to use the delete all button, it shows them all deleted. When I use the add task button after everything has been deleted, it adds my new task with all those default values once again.
How do I make the default list only appear when the site first loads? I am trying to delete the default load values, but there is a loop in here that makes them keep reappearing. Any help is greatly appreciated.