I am working on a task list manager for my class that I am in. I have to include sessions and I need to push each task into an array and be able to delete it from the array.
I think I am doing it correctly,but when I run the program, the task list shows up as empty. I think I may not be adding the tasks to the task-list array correctly, and I really would like a fresh set of eyes to take a look, because maybe you will see what I haven’t.
index.php
<?php
if (isset($_POST['tasklist'])) {
$task_list = $_POST['tasklist'];
} else {
$task_list = array();
}
// put the array in a session variable
$_SESSION['tasks']=$task_list;
$_SESSION['errors']=$errors;
//start the session to last one year
$lifetime = 60 * 60 * 24 * 365; // 1 year in seconds
ini_set('session.gc_maxlifetime', $lifetime);
//session_set_cookie_params($lifetime, '/');
session_start();
$errors = array();
switch( $_POST['action'] ) {
case 'add':
$new_task = $_POST['newtask'];
if (empty($new_task)) {
$_SESSION['errors'] = 'The new task cannot be empty.';
} else {
$_SESSION['tasks'] = $new_task;
}
break;
case 'delete':
$task_index = $_POST['taskid'];
unset($task_list[$task_index]);
$_SESSION['tasks'] = array_values($_SESSION['tasks']);
break;
}
include('task_list.php');
?>
task_list.php
<?php
session_start(); // Access the current session.
?>
</head>
<body>
<div id="page">
<div id="header">
<h1>Task List Manager</h1>
</div>
<div id="main">
<!-- part 1: the errors -->
<?php if (count($_SESSION['errors']) > 0) : ?>
<h2>Errors</h2>
<ul>
<?php foreach($_SESSION['errors'] as $error) : ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- part 2: the tasks -->
<h2>Tasks</h2>
<?php if (count($_SESSION['tasks']) == 0) : ?>
<p>There are no tasks in the task list.</p>
<?php else: ?>
<ul>
<?php foreach($_SESSION['tasks'] 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($_SESSION['tasks'] as $task) :
array_push($_SESSION['tasks'],$task);
endforeach; ?>
<!--<input type="hidden" name="action" value="add"/> -->
<label>Task:</label>
<input type="text" name="<?php $_SESSION['tasks'] ?>" id="newtask" value="<?php echo $task; ?>" /> <br />
<label> </label>
<input type="submit" value="Add Task"/>
</form>
<br />
<!-- part 4: the delete form -->
<?php if (count($_SESSION['tasks']) > 0) : ?>
<h2>Delete Task</h2>
<form action="." method="post" >
<?php foreach($_SESSION['tasks'] as $task) :
$_SESSION['tasks']=array_diff($_SESSION['tasks'],$task);
endforeach; ?>
<!--<input type="hidden" name="action" value="delete"/> -->
<label>Task:</label>
<select name="taskid">
<?php foreach($_SESSION['tasks'] as $id => $task) : ?>
<option value="<?php echo $id; ?>">
<?php echo $task; ?>
</option>
<?php endforeach; ?>
</select>
<br />
<label> </label>
<input type="submit" value="Delete Task"/>
</form>
<?php endif; ?>
</div><!-- end main -->
</div><!-- end page -->
</body>
</html>
You need to call
session_start()before any interaction with the session. So if the files are accessed separately then you need it in both. Iftask_list.phpis only included inindex.phpthen just move it to the top ofindex.php. Additionally, in your logic for theaddaction you dont have your syntax right to add a task to$_SESSION['tasks']array, instead you are overwriting it with whatever is in$new_task.