Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In addition to the PHP code below, I wish to add a Remove Task button after the

ID: 3702990 • Letter: I

Question

In addition to the PHP code below, I wish to add a Remove Task button after the Add Task button in the Add Task portion of the page. Then, modify the PHP for the page so the Add Task button adds the task to the top of the task list (currently, it adds the task to the bottom of the list), and the Remove Task button removes the task at the top of the list (LIFO).

<?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();
  
// add some hard-coded starting values to make testing easier
// $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 {
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 'Modify Task':   
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':
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.';
} elseif($i === NULL || $i === FALSE) {
$errors[] = 'The task cannot be modified.';   
} else {
$task_list[$i] = $modified_task;
$modified_task = '';
}
break;   
  
// case 'Cancel Changes':
case 'Cancel Changes':
$modified_task = '';
break;
  
// case 'Promote Task':
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.';
} elseif ($task_index == 0) {
$errors[] = 'You cannot 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':
case 'Sort Tasks':
sort($task_list);
break;

}

include('task_list.php');
?>

<!DOCTYPE html>
<html>
<head>
<title>Task List Manager</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<header>
<h1>Task List Manager</h1>
</header>

<main>
<p><?php print_r($task_list); ?></p>
  
<!-- part 1: the errors -->
<?php if (count($errors) > 0) : ?>
<h2>Errors:</h2>
<ul>
<?php foreach($errors as $error) : ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

<!-- part 2: the tasks -->
<h2>Tasks:</h2>
<?php if (count($task_list) == 0) : ?>
<p>There are no tasks in the task list.</p>
<?php else: ?>
<ul>
<?php foreach( $task_list as $id => $task ) : ?>
<li><?php echo $id + 1 . '. ' . $task; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<br>

<!-- part 3: the add form -->
<h2>Add Task:</h2>
<form action="." method="post" >
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]" value="<?php echo $task; ?>">
<?php endforeach; ?>
<label>Task:</label>
<input type="text" name="newtask" id="newtask"> <br>
<label>&nbsp;</label>
<input type="submit" name="action" value="Add Task">
</form>
<br>

<!-- part 4: the modify/promote/delete form -->
<?php if (count($task_list) > 0 && empty($task_to_modify)) : ?>
<h2>Select Task:</h2>
<form action="." method="post" >
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]" value="<?php echo $task; ?>">
<?php endforeach; ?>
<label>Task:</label>
<select name="taskid">
<?php foreach( $task_list as $id => $task ) : ?>
<option value="<?php echo $id; ?>">
<?php echo $task; ?>
</option>
<?php endforeach; ?>
</select>
<br>
<label>&nbsp;</label>
<input type="submit" name="action" value="Modify Task">
<input type="submit" name="action" value="Promote Task">
<input type="submit" name="action" value="Delete Task">

<br><br>
<label>&nbsp;</label>
<input type="submit" name="action" value="Sort Tasks">
</form>
<?php endif; ?>

<!-- part 5: the modify save/cancel form -->
<?php if (!empty($task_to_modify)) : ?>
<h2>Task to Modify:</h2>
<form action="." method="post" >
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]" value="<?php echo $task; ?>">
<?php endforeach; ?>
<label>Task:</label>
<input type="hidden" name="modifiedtaskid" value="<?php echo $task_index; ?>">
<input type="text" name="modifiedtask" value="<?php echo $task_to_modify; ?>"><br>
<label>&nbsp;</label>
<input type="submit" name="action" value="Save Changes">
<input type="submit" name="action" value="Cancel Changes">
</form>
<?php endif; ?>

</main>
</body>
</html>

Explanation / Answer

<?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();
  
// add some hard-coded starting values to make testing easier
$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 {
$arr = array('item2', 'item3', 'item4');
array_unshift($task_list, $new_task);   // this will add task on the top
}
breakl
case 'Remove Task':
$new_task = filter_input(INPUT_POST, 'newtask');
if (empty($new_task)) {
$errors[] = 'The task is empty.';
} else {
$arr = array('item2', 'item3', 'item4');
array_splice($task_list, 0, 1);    // This will delete from the top
}

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 'Modify Task':   
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':
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.';
} elseif($i === NULL || $i === FALSE) {
$errors[] = 'The task cannot be modified.';   
} else {
$task_list[$i] = $modified_task;
$modified_task = '';
}
break;   
  
// case 'Cancel Changes':
case 'Cancel Changes':
$modified_task = '';
break;
  
// case 'Promote Task':
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.';
} elseif ($task_index == 0) {
$errors[] = 'You cannot 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':
case 'Sort Tasks':
sort($task_list);
break;
}
include('task_list.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Task List Manager</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<header>
<h1>Task List Manager</h1>
</header>
<main>
<p><?php print_r($task_list); ?></p>
<!-- part 1: the errors -->
<?php if (count($errors) > 0) : ?>
<h2>Errors:</h2>
<ul>
<?php foreach($errors as $error) : ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- part 2: the tasks -->
<h2>Tasks:</h2>
<?php if (count($task_list) == 0) : ?>
<p>There are no tasks in the task list.</p>
<?php else: ?>
<ul>
<?php foreach( $task_list as $id => $task ) : ?>
<li><?php echo $id + 1 . '. ' . $task; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<br>
<!-- part 3: the add form -->
<h2>Add Task :</h2>
<form action="." method="post" >
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]" value="<?php echo $task; ?>">
<?php endforeach; ?>
<label>Task:</label>
<input type="text" name="newtask" id="newtask"> <br>
<label>&nbsp;</label>
<input type="submit" name="action" value="Add Task">
<input type="submit" name="action" value="Remove Task">   <!-- this will add the button-->
</form>
<br>
<!-- part 4: the modify/promote/delete form -->
<?php if (count($task_list) > 0 && empty($task_to_modify)) : ?>
<h2>Select Task:</h2>
<form action="." method="post" >
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]" value="<?php echo $task; ?>">
<?php endforeach; ?>
<label>Task:</label>
<select name="taskid">
<?php foreach( $task_list as $id => $task ) : ?>
<option value="<?php echo $id; ?>">
<?php echo $task; ?>
</option>
<?php endforeach; ?>
</select>
<br>
<label>&nbsp;</label>
<input type="submit" name="action" value="Modify Task">
<input type="submit" name="action" value="Promote Task">
<input type="submit" name="action" value="Delete Task">
<br><br>
<label>&nbsp;</label>
<input type="submit" name="action" value="Sort Tasks">
</form>
<?php endif; ?>
<!-- part 5: the modify save/cancel form -->
<?php if (!empty($task_to_modify)) : ?>
<h2>Task to Modify:</h2>
<form action="." method="post" >
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]" value="<?php echo $task; ?>">
<?php endforeach; ?>
<label>Task:</label>
<input type="hidden" name="modifiedtaskid" value="<?php echo $task_index; ?>">
<input type="text" name="modifiedtask" value="<?php echo $task_to_modify; ?>"><br>
<label>&nbsp;</label>
<input type="submit" name="action" value="Save Changes">
<input type="submit" name="action" value="Cancel Changes">
</form>
<?php endif; ?>
</main>
</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote