todolistPhp / index.php
index.php
Raw
<?php
$page_title = "Startsida";
include("includes/header.php");

// Create an object 
$task1 = new Tasks();
// Get the list 
$todoList=  $task1->getTodoList();
?>

<h2> Att göra lista </h2>
<form  method="POST"  action ="addTask.php">
    <label for="taskName" > Lägg till ny sak att göra:</label><br>
    <input type="text" name="taskName" id="taskName"><br>
    <span style="color: red">
        <?php
        if(isset($_SESSION['errorNamemsg'])){
            echo $_SESSION['errorNamemsg'];
        }
        unset($_SESSION['errorNamemsg']);
        ?>
    </span><br>
    <label for="deadLine" > DeadLine:</label><br>
    <input type="date" name="deadLine" id="deadLine"><br>
    <span style="color: red">
        <?php
        if(isset($_SESSION['errorDatemsg'])){
            echo $_SESSION['errorDatemsg'];
        } 
        unset($_SESSION['errorDatemsg']);
        ?>
    </span><br>
    <input class="addBtn" type="submit" value="Lägg till"> <br>
</form>

<ul class='todolist'>
    <?php
    foreach ($todoList as $todoKey => $todo ) {
        echo "<li>" . $todo['taskName'] . " ," ." Deadline: ". $todo['deadLine'] .
        "<a  href='index.php?delete=$todoKey'
        style='padding: 8px;
        font-weight: bold;
        border-radius: 4px;
        border: none;
        background-color: #d11a2a;
        color: #f0ebd8;
        width: 100px;
        font-size: 0.8em;
        float: right;
        text-align:center;
        '> Klar </a> </li>"; 
    }
    ?>
</ul>

<?php
// delete a task by click on the delete button
if(isset($_GET['delete'])){
    $index = $_GET['delete'];
    // get the json file 
    $file = file_get_contents('todolist.json');
    // convert it to array
    $todoList = json_decode($file, true);
    // delete the element from the array
    unset($todoList[$index]);
    // save the array in json after re-index 
    file_put_contents('todolist.json', json_encode(array_values($todoList), JSON_PRETTY_PRINT));
    header("location: index.php");
}
?>

<form method="POST" action='deleteAll.php'>
    <input type="submit" name ="deleteAll" value="Rensa" class="deleteAll" >
</form>

<?php
    include("includes/footer.php"); 
?>