Skip to content

Instantly share code, notes, and snippets.

@HananoshikaYomaru
Created December 12, 2023 01:38
Show Gist options
  • Save HananoshikaYomaru/a765b97c8e575714647c47ef9465c8b9 to your computer and use it in GitHub Desktop.
Save HananoshikaYomaru/a765b97c8e575714647c47ef9465c8b9 to your computer and use it in GitHub Desktop.
simple php todolist
<?php
session_start();
// Initialize the todo list
if (!isset($_SESSION['todos'])) {
$_SESSION['todos'] = [];
}
// Add a new todo item
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['new_todo'])) {
array_unshift($_SESSION['todos'], ['task' => $_POST['new_todo'], 'done' => false]);
}
// Toggle the done status
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['toggle'])) {
$index = $_GET['toggle'];
$_SESSION['todos'][$index]['done'] = !$_SESSION['todos'][$index]['done'];
usort($_SESSION['todos'], function ($a, $b) {
return $b['done'] <=> $a['done'];
});
}
// Remove a todo item
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['remove'])) {
$index = $_GET['remove'];
array_splice($_SESSION['todos'], $index, 1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple To-Do List</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<h1>Simple To-Do List</h1>
<form method="post" action="index.php">
<input type="text" name="new_todo" required>
<button type="submit">Add</button>
</form>
<div>
<?php foreach ($_SESSION['todos'] as $index => $todo) : ?>
<div class="todo-item">
<span class="<?= $todo['done'] ? 'strikethrough' : '' ?>">
<?= htmlspecialchars($todo['task']) ?>
</span>
<a href="?toggle=<?= $index ?>">[<?= $todo['done'] ? 'Uncheck' : 'Check' ?>]</a>
<a href="?remove=<?= $index ?>">[Remove]</a>
</div>
<?php endforeach; ?>
</div>
</body>
</html>
.strikethrough {
text-decoration: line-through;
}
.todo-item {
margin-bottom: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment