-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
93 lines (89 loc) · 2.25 KB
/
index.php
File metadata and controls
93 lines (89 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
require 'vendor/autoload.php';
# This logic handles connecting to the database, where we store our todo status
$pdo = new \PDO("sqlite:" . "db/sqlite.db");
# This PHP logic handles user actions
# New TODO
if (isset($_POST['submit']))
{
$description = $_POST['description'];
$sth = $pdo->prepare("INSERT INTO todos (description) VALUES (:description)");
$sth->bindValue(':description', $description, PDO::PARAM_STR);
$sth->execute();
}
# Delete TODO
elseif (isset($_POST['delete']))
{
$id = $_POST['id'];
$sth = $pdo->prepare("delete from todos where id = :id");
$sth->bindValue(':id', $id, PDO::PARAM_INT);
$sth->execute();
}
# Update completion status
elseif (isset($_POST['complete']))
{
$id = $_POST['id'];
$sth = $pdo->prepare("UPDATE todos SET complete = 1 where id = :id");
$sth->bindValue(':id', $id, PDO::PARAM_INT);
$sth->execute();
}
# Here is the HTML:
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Todo List</title>
</head>
<body class="container">
<h1>Todo List</h1>
<form method="post" action="">
<input type="text" name="description" value="">
<input type="submit" name="submit" value="Add">
</form>
<h2>Current Todos</h2>
<table class="table table-striped">
<thead><th>Task</th><th></th><th></th></thead>
<tbody>
<?php
# Entering PHP mode,
$sth = $pdo->prepare("SELECT * FROM todos ORDER BY id DESC");
$sth->execute();
foreach ($sth as $row) {
# Exiting PHP Mode
?>
<tr>
<td>
<!-- This is PHP shorthand for inserting dynamic text into HTML -->
<?=htmlspecialchars($row['description'])?></td>
<td>
<?php # Here we are mixing HTML and PHP to get the desired document
if (!$row['complete']) {
?>
<form method="POST">
<button type="submit" name="complete">Complete</button>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="hidden" name="complete" value="true">
</form>
<?php
} else {
?>
Task Complete!
<?php
}
?>
</td>
<td>
<form method="POST">
<button type="submit" name="delete">Delete</button>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="hidden" name="delete" value="true">
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>