Skip to content

Instantly share code, notes, and snippets.

@cladjidane
Created November 13, 2023 10:11
Show Gist options
  • Save cladjidane/a644f017587b9e6685a767725e96ccbb to your computer and use it in GitHub Desktop.
Save cladjidane/a644f017587b9e6685a767725e96ccbb to your computer and use it in GitHub Desktop.
Todo List PHP - chapitre 4 - fin
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
session_start();
$notice = '';
$_SESSION['tasks'] = $_SESSION['tasks'] ?? [];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_POST['mode'] === 'add') {
$taskName = !empty($_POST['field-task']) ? $_POST['field-task'] : ($_POST['select-task'] ?? '');
if (empty($taskName)) {
$notice = "Le nom de la tâche est vide !";
} else {
$taskId = uniqid();
$_SESSION['tasks'][$taskId] = ['task' => $taskName, 'id' => $taskId, 'status' => 'wip'];
$notice = "Tâche ajoutée avec succès !";
}
}
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['mode'], $_GET['id'])) {
$taskId = $_GET['id'];
if ($_GET['mode'] === 'update') {
$_SESSION['tasks'][$taskId]['status'] = $_SESSION['tasks'][$taskId]['status'] === 'wip' ? 'finish' : 'wip';
} elseif ($_GET['mode'] === 'delete' && $_SESSION['tasks'][$taskId]['status'] === 'finish') {
unset($_SESSION['tasks'][$taskId]);
}
}
$tasks = $_SESSION['tasks'];
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Todo</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Todo List</h1>
<h2>Gestionnaire de tâches</h2>
<form class="form-add" method="post" action="">
<label for="item">Ajouter une tâche</label>
<div class="fields">
<input name="field-task" type="text" id="item" placeholder="Intitulé de la tâche" />
ou
<select name="select-task">
<option value="">Choisir une tâche</option>
<option value="Passer le balai">Passer le balai</option>
<option value="Saluer le boss">Saluer le boss</option>
<option value="Couper l'ordi">Couper l'ordi</option>
</select>
</div>
<input type="hidden" name="mode" value="add" />
<button type="submit">Ajouter</button>
</form>
<?php if($notice != null) : ?>
<div class="notice"><?php echo $notice; ?></div>
<?php endif; ?>
<ul class="list-todo">
<?php if($tasks) : ?>
<?php foreach($tasks as $key => $task) : ?>
<li class="<?php echo $task['status'] == "finish" ? "ok": ""; ?>">
<a href="?mode=update&id=<?php echo $task['id']; ?>">
<input
<?php echo $task['status'] == "finish" ? "checked": ""; ?>
type="checkbox"
name="status"
/>
</a>
<span class="<?php echo $task['status'] == "finish" ? "finished": "wip"; ?>">
<?php echo $task['task']; ?> (<?php echo $task['id']; ?> - <?php echo $task['status']; ?>)
</span>
<a href="?mode=delete&id=<?php echo $task['id']; ?>" class="btdelete"></a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</body>
</html>
@cladjidane
Copy link
Author

`/*
Organisation
1-Positionnement
2-Dimensions
3-Texte
4-Bordures et fonds
5-Propriétés CSS3 ou spécifiques navigateurs
*/

*,
::before,
::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}

body {
min-height: 100vh;
font-family: Arial, Helvetica, sans-serif;
background: radial-gradient(rgb(233, 233, 233), rgb(187, 187, 187));
}

h1 {
margin: 30px 0;
text-align: center;
font-size: 30px;
}

h2 {
margin-bottom: 50px;
text-align: center;
font-size: 20px;
}

.notice {
padding: 20px;
width: 70%;
max-width: 1200px;
min-width: 320px;
margin: 20px auto 0;
border: 1px solid black;
border-radius: 10px;
background: rgb(178, 233, 177);
}

.list-todo {
width: 70%;
max-width: 1200px;
min-width: 320px;
margin: 20px auto 0;
list-style-type: none;
}

.list-todo li {
display: flex;
align-items: center;
width: 100%;
margin: 5px 0;
padding: 10px;
color: #333;
font-size: 15px;
background: #f1f1f1;
border-radius: 10px;
}

.list-todo li input {
width: 20px;
height: 20px;
margin-left: 10px;
pointer-events: none;
}

.list-todo li .btdelete {
height: 20px;
width: 20px;
margin-left: auto;
padding: 0;
background: transparent;
background-image: url('./ressources/fermer.svg');
border: none;
cursor: pointer;
}

.list-todo li .btdelete:hover {
opacity: .5;
}

.list-todo li span {
margin-left: 20px;
font-size: 15px;
}

.list-todo li.ok span {
text-decoration: line-through;
}

.form-add {
display: block;
max-width: 1200px;
min-width: 320px;
width: 70%;
margin: 0 auto;
padding: 20px;
background: white;
border-radius: 10px;
}

.form-add .fields {
display: flex;
align-items: center;
gap: 20px;
}

.form-add label {
display: block;
color: black;
font-size: 15px;
font-weight: bold;
}

.form-add select {
width: 50%;
margin: 10px 0;
padding: 12px 10px;
font-size: 15px;
}

.form-add input {
width: 50%;
margin: 10px 0;
padding: 10px;
font-size: 15px;
}

.form-add button {
padding: 10px;
font-size: 15px;
}

.form-checked {
display: flex;
align-items: center;
gap: 5px;
}

.form-checked button {
border: none;
background: silver;
font-size: 10px;
padding: 4px 3px;
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment