1

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.

7
  • How do I make it so $task_list is set as the initial values, but not as default values? Commented Mar 1, 2016 at 5:31
  • 1
    You might have to store a cookie to do a one-time thing like that. Commented Mar 1, 2016 at 5:46
  • There is a way to do it without cookies, but I'm not sure how. Our next assignment is likely going to be learning cookies, but this one we are required to do without it. Commented Mar 1, 2016 at 6:03
  • localStorage? developer.mozilla.org/en-US/docs/Web/API/Window/localStorage Commented Mar 1, 2016 at 6:14
  • Save the user ip (user agent) to database? That is not always accurate because you can spoof it. Commented Mar 1, 2016 at 6:15

1 Answer 1

2

I think I solved your problem... which is an unusual one, as I don't think it's practical.

I kept asking myself, "What's different about the first load of the page, and every other load after something has happened" and the answer I came to was the $action. If you only re-write the elements in your array when there is no "$action", then you're only re-writing the elements on the first load of the page. So I've separated your opening if statement into two, and added the call for $action's INPUT_POST above, so that the if statement has something to compare.

$task_list = filter_input(INPUT_POST, 'tasklist', 
    FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
$action = filter_input(INPUT_POST, 'action');
if ($task_list === NULL) {
$task_list = array();
}   
if ($action == NULL) {    
    $task_list[] = 'Write chapter';
    $task_list[] = 'Edit chapter';
    $task_list[] = 'Proofread chapter';
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I needed. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.