0

I'm making a To do app for school with PHP. The problem i'm facing: I have 3 documents: index.php, application.php and CSS

My problem: every time I add a new task, my array is empty again and will only show the latest added item.

I think the problem is that "application.php" loads again every time I add a task. But I'm not sure how to fix that.

Index.php:

<form action="/Periodeopdracht/index.php" method="POST">
<div class="headerToDo">
    <input class="addText title" type="text" value="Click to add a to do" name="nextToDo">
    <input class="clickablePlus" type="submit" value="+" name="submit">
</div>
</form>

<?php if(!$empty): ?>
    <?php foreach ($newToDo as $toDo): ?>
    <div class="toDo">
        <div class="textToDo"><?= $toDo ?></div>
    </div>
    <?php endforeach ?>
<?php endif ?>

Application.php:

<?php

$GLOBALS['empty'] = true;
$GLOBALS['newToDo'] = array();

if(isset($_POST["submit"]))
{
    $empty = false;
    array_unshift($newToDo, $_POST["nextToDo"]);
}

var_dump($newToDo);

?>
RW24
  • 624
  • 2
  • 10
  • 19

2 Answers2

0

It looks like you aren't saving the current todo list anywhere

What your app could do this (in very simple terms, if you wanted to store the list in the session)

session_start();

$_SESSION['todoList'] = isset($_SESSION['todoList']) ? $_SESSION['todoList'] : array();

if(isset($_POST["submit"]))
{
    $empty = false;
    array_unshift($_SESSION['todoList'], $_POST["nextToDo"]);
}
bear
  • 11,364
  • 26
  • 77
  • 129
  • please **don't** use sessions... when the session is gone, you'll lose all the data. you really should store this stuff in a database. – Karoly Horvath Nov 12 '14 at 12:52
  • @KarolyHorvath hence why the **if you wanted to store the list in the session**. Depending on the needs of this system, it shouldn't be overengineered. – bear Nov 12 '14 at 12:53
  • For a school assignment, sessions will do fine. – Jorg Nov 12 '14 at 12:54
  • @bear: expecting your to-do list be there the next time, or from another browser/device is definitely **not** overengineering. – Karoly Horvath Nov 12 '14 at 12:55
  • @KarolyHorvath can you list me the specifications of this system? – bear Nov 12 '14 at 12:56
  • @Ruben Wouters $_SESSION['todoList'] will become the array. – Chilion Nov 12 '14 at 12:58
  • @RubenWouters what the code now does (I've just updated it), is start the session (or reuse it). It then checks to see if a variable named `todoList` exists in the session, if it does use it, else, initialise it as an array. Of course, it could be improved. It then adds the item to the top of the array with `array_unshift`. – bear Nov 12 '14 at 12:59
  • @RubenWouters read about `inline if` http://stackoverflow.com/questions/1506527/how-do-i-use-shorthand-if-else – Berriel Nov 12 '14 at 13:00
0

U could use a simple text file to store (append!) the new data at the end of the file if u can not use a database to store the existing tasks.

U also can easily read the content of the text file to display the existing data, for example each row == one task

save data

$fh = fopen('tasks.txt', 'a');
fwrite($task, $fh);
fclose($fh);

display data

$fh = fopen('tasks.txt', 'r');
$content = fread($fh, filesize('tasks.txt'));
fclose($fh);

Just as an idea. Make sure each task is in one row so u can easily go over the $content with for loop