-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.php
134 lines (98 loc) · 3.41 KB
/
todo.php
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php session_start() ?>
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+" crossorigin="anonymous"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To Do App</title>
<link rel="stylesheet" href="style.css">
</head>
<?php
if (!isset($_COOKIE['PHPSESSID'])) {
header("Location: register.php");
return;
} else {
if (!isset($_SESSION['userId'])) {
header("Location: register.php");
return;
}
}
?>
<?php
try {
$pdo = new PDO("sqlite:database/todo.db");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->query(
"CREATE TABLE IF NOT EXISTS tasks (taskId INTEGER PRIMARY KEY, userId INTEGER, description TEXT)"
);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
if (isset($_POST["add"])) {
$id = $_SESSION['userId'];
$value = $_POST['value'];
try {
$insert = $pdo->prepare(
"INSERT INTO tasks (userId, description) VALUES (:userId, :value)"
);
$insert->execute([":userId" => $id, ":value" => $value]);
header("Location: todo.php");
return;
} catch (PDOException $e) {
echo "Todo creation failed: " . $e->getMessage();
}
}
if(isset($_GET["delete"])){
$id = $_GET['taskId'];
$pdo->exec("DELETE from tasks Where taskId = $id");
}
$search = isset($_GET['search']) ? $_GET['search']: "";
$id = $_SESSION['userId'];
$tasks = $pdo->query("SELECT * FROM tasks WHERE userId = $id")->fetchAll(PDO::FETCH_ASSOC);
if($search != ""){
$tasks = $pdo->query("SELECT * FROM tasks WHERE userId = $id AND description LIKE '%$search%'")->fetchAll(PDO::FETCH_ASSOC);
}
?>
<body>
<div class="filter">
<form action="./todo.php" method="GET">
<input type="text" name="search" placeholder="search">
<input type="submit" value="search">
</form>
<a href="todo.php">temizle</a>
</div>
<div class="container">
<form action="./todo.php" method="post" class="todo_form">
<h2 class="to_do_title">TO DO LIST</h2>
<div class="todo_div">
<input name="value" type="text" class="todo_input" placeholder="Add Content">
<button name="add" type="submit" class="todo_button">Add</button>
</div>
</form>
<div class="tasks">
<div class="task">
<div class="id bold">ID</div>
<div class="description bold">Description</div>
<div class="action bold">Action</div>
</div>
<?php
$count = 1;
foreach($tasks as $task){
$id = $task['taskId'];
?>
<div class="task">
<div class="id"><?= $count ?></div>
<div class="description"><?= $task['description'] ?></div>
<div class="action">
<a href=></a>
<a href=<?="todo.php?delete=delete&taskId=$id"?>><button>Delete</button></a>
</div>
</div>
<?php $count++; } ?>
</div>
</div>
</body>
</html>