Created
September 5, 2022 20:20
-
-
Save jpfyoder/71e90ce92f9c955503e1a65cfd0bb703 to your computer and use it in GitHub Desktop.
Very very basic todo application with no javascript using PHP and forms :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# MySQL connection | |
$mysql = mysqli_connect("db", "root", "example"); | |
# DDL | |
$mysql->query("CREATE DATABASE IF NOT EXISTS demo;"); | |
$mysql->query("USE demo;"); | |
$mysql->query("CREATE TABLE IF NOT EXISTS todos(tid INT PRIMARY KEY AUTO_INCREMENT, is_checked BOOLEAN, message TEXT);"); | |
# Prepared Statements | |
$newStmt = $mysql->prepare("INSERT INTO todos(is_checked, message) VALUES (false, ?);"); | |
$checkStmt = $mysql->prepare("UPDATE todos SET is_checked = !is_checked WHERE tid = ?;"); | |
$editStmt = $mysql->prepare("UPDATE todos SET message = ? WHERE tid = ?;"); | |
$deleteStmt = $mysql->prepare("DELETE FROM todos WHERE tid = ?;"); | |
# insert new items | |
if (isset($_POST["submitNew"])) { | |
$newStmt->bind_param("s", $_POST["newItem"]); | |
$newStmt->execute(); | |
} | |
# process edited items | |
if (isset($_POST["submitMod"])) { | |
$editStmt->bind_param("si", $_POST["modItem"], $_POST["submitMod"]); | |
$editStmt->execute(); | |
} | |
# check checked items | |
if (isset($_POST["check"])) { | |
$checkStmt->bind_param("i", $_POST["check"]); | |
$checkStmt->execute(); | |
} | |
# delete deleted items | |
if (isset($_POST["delete"])) { | |
$deleteStmt->bind_param("i", $_POST["delete"]); | |
$deleteStmt->execute(); | |
} | |
# grab items from database | |
$todos = []; | |
$query = $mysql->query("SELECT tid, is_checked, message FROM todos;"); | |
while($row = $query->fetch_assoc()) { | |
# flag items to edit | |
$edit_mode = false; | |
if (isset($_POST["edit"]) && $row['tid'] == $_POST["edit"]) { | |
$edit_mode = true; | |
} | |
$todos[] = [ | |
"id" => $row['tid'], | |
"text" => $row['message'], | |
"is_checked" => $row['is_checked'], | |
"edit_mode" => $edit_mode | |
]; | |
} | |
# done | |
$_POST = array(); | |
?> | |
<html> | |
<head> | |
<title>Todo List PHP</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous"> | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Todo List PHP</h1> | |
<form action="index.php" method="post" id="todoForm"></form> | |
<ul class="list-group"> | |
<?php foreach ($todos as $todo): ?> | |
<?php if ($todo["edit_mode"] == false): ?> | |
<li class="list-group-item d-flex justify-content-between"> | |
<div class="align-self-center"> | |
<button type="submit" form="todoForm" class="btn btn-<?php echo $todo['is_checked'] ? 'dark bi bi-x' : 'light bi bi-check'?>" name="check" value=<?=$todo['id']?>></button> | |
<?php echo $todo['is_checked']? '<s>' . $todo['text'] . '</s>' : $todo['text'] ?> | |
</div> | |
<div> | |
<button type="submit" form="todoForm" class="btn btn-warning bi bi-pencil" name="edit" value=<?=$todo['id']?>></button> | |
<button type="submit" form="todoForm" class="btn btn-danger bi bi-trash" name="delete" value=<?=$todo['id']?>></button> | |
</div> | |
</li> | |
<?php else: ?> | |
<li class="list-group-item d-flex justify-content-between"> | |
<textarea class="form-control" name="modItem" rows="1" form="todoForm"><?=$todo['text']?></textarea> | |
<button type="submit" form="todoForm" class="btn btn-primary bi bi-check ms-2" name="submitMod" value=<?=$todo['id']?>></button> | |
</li> | |
<?php endif; endforeach; ?> | |
<li class="list-group-item d-flex justify-content-between"> | |
<textarea class="form-control" name="newItem" rows="1" form="todoForm"></textarea> | |
<button type="submit" form="todoForm" class="btn btn-success bi bi-plus ms-2" name="submitNew"></button> | |
</li> | |
</ul> | |
</div> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment