-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtask.php
More file actions
85 lines (73 loc) · 2.09 KB
/
Copy pathtask.php
File metadata and controls
85 lines (73 loc) · 2.09 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
<?php
function get_connection(){
$dsn = "mysql:host=localhost;dbname=wftutorials";
$user = "root";
$passwd = "";
$conn = new PDO($dsn, $user, $passwd);
return $conn;
}
function get_parent_tasks(){
$results = [];
try{
$conn = get_connection();
$results = $conn->query("SELECT * from all_tasks WHERE parent is NULL");
}catch (Exception $e){
}
return $results;
}
function get_child_tasks($id){
$results = [];
try{
$conn = get_connection();
$results = $conn->query("SELECT * from all_tasks WHERE parent=". $id);
}catch (Exception $e){
}
return $results;
}
if(isset($_POST['save-task'])){
$task = isset($_POST['task']) ? $_POST['task'] : null;
$parent = isset($_POST['parent']) ? $_POST['parent'] : null;
try{
$conn = get_connection();
if($parent && is_numeric($parent)){
$sql = "INSERT INTO all_tasks(`parent`, `task`) VALUES (?,?)";
$query = $conn->prepare($sql);
$query->execute([$parent, $task]);
}else if($task){
$sql = "INSERT INTO all_tasks(`task`) VALUES (?)";
$query = $conn->prepare($sql);
$query->execute([$task]);
}
}catch (Exception $e){
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Task Builder</title>
</head>
<body>
<h3>Add a new task</h3>
<form method="post">
<select name="parent">
<option selected value>-- Select a parent --</option>
<?Php foreach(get_parent_tasks() as $task):?>
<option value="<?php echo $task["id"];?>"><?Php echo $task["task"];?></option>
<?php endforeach; ?>
</select>
<input name="task" type="text" />
<button type="submit" name="save-task">Save Task</button>
</form>
<ul>
<?Php foreach(get_parent_tasks() as $task):?>
<li><?php echo $task["task"];?>
<?php foreach(get_child_tasks($task["id"]) as $child):?>
<br> ∗ <?php echo $child["task"];?>
<?php endforeach;?>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>