Menu

Php Arrays Modify Indexphp File Starts Session Lasts 1 Year B Modify Indexphp File Stores Q43900525

PHP arrays!

a. Modify the index.php file so it starts a session that lasts for1 year.
b. Modify the index.php file so it stores the array of tasks in thesession.
c. Modify the task_list.php file so it does not store the array oftasks in hidden HTML fields.
d. Test this application. Make sure to exit and restart the browserto make sure the session is persistent.

CODE:

index.php

<?php
$task_list = filter_input(INPUT_POST, ‘tasklist’,FILTER_DEFAULT,
       FILTER_REQUIRE_ARRAY);
if ($task_list === NULL) {
    $task_list = array();
}
$action = filter_input(INPUT_POST, ‘action’);
$errors = array();

switch( $action ) {
    case ‘add’:
        $new_task =filter_input(INPUT_POST, ‘newtask’);
        if (empty($new_task)){
           $errors[] = ‘The new task cannot be empty.’;
        } else {
           $task_list[] = $new_task;
        }
        break;
    case ‘delete’:
        $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;
}

include(‘task_list.php’);
?>

task_list.php

<!DOCTYPE html>
<html>
<head>
    <title>Task ListManager</title>
    <link rel=”stylesheet” type=”text/css”href=”main.css”/>
</head>
<body>
    <header>
        <h1>Task ListManager</h1>
    </header>
    <main>
        <!– part 1: theerrors –>
        <?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: thetasks –>
       <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: theadd form –>
        <h2>AddTask</h2>
        <form action=”.”method=”post” >
           <?php foreach($task_list as $task) : ?>
             <input type=”hidden” name=”tasklist[]” value=”<?php echo$task; ?>”>
           <?php endforeach; ?>
           <input type=”hidden” name=”action” value=”add”>
           <label>Task:</label>
           <input type=”text” name=”newtask” id=”newtask” ><br>
           <label>&nbsp;</label>
           <input type=”submit” value=”Add Task”>
        </form>
        <br>

        <!– part 4: thedelete form –>
        <?php if(count($task_list) > 0) : ?>
        <h2>DeleteTask</h2>
        <form action=”.”method=”post” >
           <?php foreach($task_list as $task) : ?>
             <input type=”hidden” name=”tasklist[]” value=”<?php echo$task; ?>”>
           <?php endforeach; ?>
           <input type=”hidden” name=”action” value=”delete”>
           <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” value=”Delete Task”>
        </form>
        <?php endif;?>
    </main>
</body>
</html>

Expert Answer


Answer to PHP arrays! a. Modify the index.php file so it starts a session that lasts for 1 year. b. Modify the index.php file so i…

OR