-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.php
155 lines (142 loc) · 4.98 KB
/
index.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/*
* Example of using r_tree_traversal() MySQL stored procedure.
*/
$dsn = 'mysql:host=127.0.0.1;dbname=tree-traversal';
$pdo = new PDO($dsn, 'root', 'root', array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
));
//inserting node
if(isset($_POST['insert'])) {
$sql = "CALL r_tree_traversal('insert', NULL, {$_POST['parent_id']});";
$prep = $pdo->prepare($sql);
$prep->execute();
$newNodeId = (int) $prep->fetchColumn();
$sql = "INSERT INTO tree_content (node_id, name) VALUES (?,?)";
$prep = $pdo->prepare($sql);
$prep->execute(array($newNodeId, $_POST['node_name']));
}
//deleting node
if(isset($_POST['delete'])) {
$sql = "CALL r_tree_traversal('delete', {$_POST['node_id']}, NULL);";
$prep = $pdo->prepare($sql);
$prep->execute();
}
//moving node
if(isset($_POST['move']) && ($_POST['node_id'] != $_POST['new_parent_id'])) {
$sql = "CALL r_tree_traversal('move', {$_POST['node_id']}, {$_POST['new_parent_id']});";
$prep = $pdo->prepare($sql);
$prep->execute();
}
//order node in branch = same parent_id
if(isset($_POST['order']) && ($_POST['node_id'] != $_POST['under_node_id'])) {
$sql = "CALL r_tree_traversal('order', {$_POST['node_id']}, {$_POST['under_node_id']});";
$prep = $pdo->prepare($sql);
$prep->execute();
}
//for HTML selects
$sql = "CALL r_return_tree(NULL, 'en');";
$prep = $pdo->prepare($sql);
$prep->execute();
$selectOptions = $prep->fetchAll(PDO::FETCH_OBJ);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<title>Tree Traversal Examples</title>
</head>
<body>
<div class="container">
<div class="page-header">
<h1><span class="glyphicon glyphicon-tree-deciduous"></span> Tree Traversal Examples</h1>
<p><a href="https://github.com/werc/TreeTraversal">github.com/werc/TreeTraversal</a></p>
</div>
<pre>
<?php
//current tree structure
$tree = '';
foreach ($selectOptions as $key => $row) {$tree .= sprintf('%s' . PHP_EOL, $row->name);}
echo rtrim($tree, PHP_EOL);
?>
</pre>
<br>
<h3>Insert node</h3>
<?php
echo '<form method="POST" class="form-inline">';
echo '<div class="form-group"><input type="text" name="node_name" class="form-control" placeholder="Name"></div>';
echo '<div class="form-group"> ';
echo '<select name="parent_id" class="form-control">';
printf('<option value="%s">%s</option>', '', '- parent -');
foreach ($selectOptions as $key => $row) {
printf('<option value="%s">%s</option>', $row->node_id, $row->name);
}
echo '</select></div>';
echo ' <button type="submit" name="insert" class="btn btn-default">Insert</button>';
echo '</form>';
?>
<br>
<h3>Delete node</h3>
<?php
echo '<form method="POST" class="form-inline">';
echo '<div class="form-group">';
echo '<select name="node_id" class="form-control">';
foreach ($selectOptions as $key => $row) {
if($row->node_id > 1) { //do not delete root
printf('<option value="%s">%s</option>', $row->node_id, $row->name);
}
}
echo '</select></div>';
echo ' <button type="submit" name="delete" class="btn btn-default">Delete</button>';
echo '</form>';
?>
<br>
<h3>Move node and leaves (if any)</h3>
<?php
echo '<form method="POST" class="form-inline">';
echo '<div class="form-group">';
echo '<select name="node_id" class="form-control">';
printf('<option value="%s">%s</option>', '', '- move -');
foreach ($selectOptions as $key => $row) {
if($row->node_id > 1) { //do not move root
printf('<option value="%s">%s</option>', $row->node_id, $row->name);
}
}
echo '</select></div>';
echo '<div class="form-group"> ';
echo '<select name="new_parent_id" class="form-control">';
printf('<option value="%s">%s</option>', '', '- new parent -');
foreach ($selectOptions as $key => $row) {
printf('<option value="%s">%s</option>', $row->node_id, $row->name);
}
echo '</select></div>';
echo ' <button type="submit" name="move" class="btn btn-default">Move</button>';
echo '</form>';
?>
<br>
<h3>Order in branch</h3>
<?php
echo '<form method="POST" class="form-inline">';
echo '<div class="form-group">';
echo '<select name="node_id" class="form-control">';
printf('<option value="%s">%s</option>', '', '- order -');
foreach ($selectOptions as $key => $row) {
if($row->node_id > 1) { //do not move root
printf('<option value="%s">%s</option>', $row->node_id, $row->name);
}
}
echo '</select></div>';
echo '<div class="form-group"> ';
echo '<select name="under_node_id" class="form-control">';
printf('<option value="%s">%s</option>', '', '- under node with same parent! -');
foreach ($selectOptions as $key => $row) {
printf('<option value="%s">%s</option>', $row->node_id, $row->name);
}
echo '</select></div>';
echo ' <button type="submit" name="order" class="btn btn-default">Order</button>';
echo '</form>';
?>
</div>
</body>
</html>