仅向PHP数组添加一次值

时间:2016-02-28 23:26:14

标签: php arrays

我正在处理涉及PHP数组的任务。这里没有使用数据库,因此任何存储的信息都只存储在名为$ task_list的数组中。程序首次加载时,三个项目将添加到任务列表数组中,并在index.php文件中包含以下代码:

$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';
}

我在不同的文件上有一个HTML表单,这是用户实际与之交互的文件。表单上的操作将传递到我的index.php文件,我在其中设置了一个开关设置来处理每个操作。

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':
        $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':
        $value = 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 ($value === NULL || $value === FALSE) {
            $errors[] = 'The task cannot be modified.';
        } else {
            $task_list[$value] = $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.';
        } elseif ($task_index == 0) {
            $errors[] = 'You cannot promote the first task.';
        } else {
            $task_value = $task_list[$task_index];
            $prior_task_value = $task_list[$task_index - 1];

            $task_list[$task_index - 1] = $task_value;
            $task_list[$task_index] = $prior_task_value;
            break;
        }

    case 'Sort Tasks':
        sort($task_list);
        break;

    case 'Delete All Tasks':
        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;
}

include('task_list.php');

我大部分时间都已完成,但我遇到的问题是我仍然需要修改代码以便完成三个任务:"编写章节","编辑章节"和"校对章"应用程序首次加载时,仅添加到数组中。我遇到的问题是,当我删除列表中的所有任务然后添加新任务时,原始的三个任务再次出现,即使它们之前已被删除。我在这里做错了什么?

编辑:如果需要我的完整代码,我在这里粘贴了index.php:

http://pastebin.com/h6J7aipi

这里有task_list.php:

http://pastebin.com/PkSpC30J

0 个答案:

没有答案
相关问题