-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_view.php
231 lines (196 loc) · 7.76 KB
/
admin_view.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
session_start();
if(!isset($_SESSION['role']) || $_SESSION['role']!='admin')
{
echo '<h1>Unauthorized access!!</h1><p>Redirecting...</p>';
header('refresh: 3; index.php');
exit;
}
function addcss(){
echo '<link rel="stylesheet" type="text/css" href="styles/rest_details.css">';
echo '<link rel="stylesheet" type="text/css" href="styles/admin_page.css">';
echo '<script src="scripts/confirm_changes.js"></script>';
}
if(!isset($_GET['edit_category']))
$table = 'person';
else
$table = $_GET['edit_category'];
if(!isset($_GET['page']))
$page = 0;
else
$page = intval($_GET['page'])-1;
include 'templates/header.php';
include 'templates/navbar.php';
include 'templates/db-con.php';
$sql = "SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.key_column_usage WHERE table_schema = '$database_name' AND CONSTRAINT_NAME = 'PRIMARY';";
$result = mysqli_query($conn, $sql);
$primary_keys = array();
if (mysqli_num_rows($result) != 0)
{
while($data = mysqli_fetch_assoc($result))
$primary_key[$data['TABLE_NAME']] = $data['COLUMN_NAME'];
}
$sql = "SELECT count(*) from $table;";
$result = mysqli_query($conn, $sql);
$column = array();
if (mysqli_num_rows($result) != 0)
{
$data = mysqli_fetch_assoc($result);
$total_rows = $data['count(*)'];
}
$sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='$database_name' AND `TABLE_NAME`='$table';";
$result = mysqli_query($conn, $sql);
$column = array();
if (mysqli_num_rows($result) != 0)
while($data = mysqli_fetch_assoc($result))
$column[] = $data['COLUMN_NAME'];
?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">
<nav id="side-navigation">
<ul class="nav nav-pills flex-column side-nav bg-light">
<li class="nav-item <?php if($table == 'person') echo 'active';?>" id="side-nav-item"><a href="admin_view.php?edit_category=person&page=1" class="nav-link" id="side-nav-link">Users</a></li>
<li class="nav-item <?php if($table == 'rest') echo 'active';?>" id="side-nav-item"><a href="admin_view.php?edit_category=rest&page=1" class="nav-link" id="side-nav-link">Restaurants</a></li>
<li class="nav-item <?php if($table == 'review') echo 'active';?>" id="side-nav-item"><a href="admin_view.php?edit_category=review&page=1" class="nav-link" id="side-nav-link">Reviews</a></li>
<li class="nav-item <?php if($table == 'favourites') echo 'active';?>" id="side-nav-item"><a href="admin_view.php?edit_category=favourites&page=1" class="nav-link" id="side-nav-link">Favourites</a></li>
<li class="nav-item <?php if($table == 'reservations') echo 'active';?>" id="side-nav-item"><a href="admin_view.php?edit_category=reservations&page=1" class="nav-link" id="side-nav-link">Reservations</a></li>
</ul>
</nav>
</div>
<div class="col-sm-10" id="result-table">
<?php if($table=='rest'){
?>
<a href="rest_form.php?method=insert"><button class="btn btn-primary" style="margin:5px 0px;">Add New</button></a>
<?php }
elseif($table=='person'){
?><a href="profile_view.php?method=insert"><button class="btn btn-primary" style="margin:5px 0px;">Add New</button></a><?php } ?>
<table class="table table-hover table-bordered">
<thead>
<tr>
<?php
if($table!='favourites' && $table!='review' && $table!='reservations')
echo '<th>Edit</th>' ?>
<th>Delete</th>
<?php
foreach ($column as $heading)
echo "<th scope=\"col\">$heading</th>";
?>
</tr>
</thead>
<tbody>
<?php
$results_per_page = 10;
$start = $page * $results_per_page;
if (isset($_POST['search-bar'])) {
$search = mysqli_real_escape_string($conn, $_POST['search-bar']);
if($table=='rest')
$sql = "SELECT * FROM rest where rest_name like '$search%'";
elseif($table=='person')
{
$names = array();
$names = explode(" ",$search);
if(sizeof($names) == 1)
{
$x = $names[0];
$sql = "SELECT * FROM person where fname like '$x%' or lname like '$x%'";
}
else
{
$fname = $names[0];
$lname = $names[1];
$sql = "SELECT * FROM person where fname like '$fname%' or lname like '$lname%'";
}
}
elseif($table=='review' || $table=='favourites' || $table=='reservations')
{
if(is_numeric($search))
$sql = "SELECT * FROM $table where rest_id = $search;";
else
$sql = "SELECT * from $table where user_id like '$search%';";
}
else
$sql = "select * from $table LIMIT $start, $results_per_page;";
}
else
$sql = "select * from $table LIMIT $start, $results_per_page;";
$result = mysqli_query($conn, $sql);
$result_length = mysqli_num_rows($result);
if(isset($_POST['search-bar']))
$limit = mysqli_num_rows($result);
else
$limit = ($result_length < $results_per_page) ? $result_length : $results_per_page;
if ($result_length != 0) {
for ($x = 0; $x < $limit; $x++) {
$row_data = mysqli_fetch_assoc($result);
if($table == 'favourites')
echo "<tr><td><button class=\"btn btn-danger\" onclick=\"return confirm_changes()\"><a id=\"butt-link\" href=\"delete_data.php?table=favourites&user_id=".$row_data['user_id']."&rest_id=".$row_data['rest_id']."\">Delete</a></button></td>";
else if($table == 'review')
echo "<tr><td><button class=\"btn btn-danger\" onclick=\"return confirm_changes()\"><a id=\"butt-link\" href=\"delete_data.php?table=review&user_id=".$row_data['user_id']."&rest_id=".$row_data['rest_id']."\">Delete</a></button></td>";
else
{
$curr_key = $row_data[$primary_key[$table]];
echo '<tr>';
if($table != 'reservations')
{
echo '<td><button class="btn btn-primary"><a href="';
if($table == 'person')
echo "profile_view.php?user_id=".$row_data['user_id'];
else if($table == 'rest')
echo "rest_form.php?method=update&rest_id=".$row_data['rest_id'];
echo "\" id=\"butt-link\">Edit</a></button></td>";
}
echo "<td><button class=\"btn btn-danger\" onclick=\"return confirm_changes()\"><a id=\"butt-link\" href=\"delete_data.php?table=$table&id=$primary_key[$table]&value=$curr_key\">Delete</a></button></td>";
}
foreach($column as $attr)
{
$row = $row_data["$attr"];
echo "<td scope=\"col\">$row</td>";
}
echo '</tr>';
}
}
?>
</tbody>
</table>
<?php if (!isset($_POST['search-bar'])) { ?>
<nav aria-label="Results">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="
<?php
if($page == 0)
$prev = 1;
else
$prev = $page;
$last_page = ceil($total_rows/$results_per_page);
$start_index = $page-3>1 ? $page-3 : 1;
$end_index = $page+5<$last_page ? $page+5 : $last_page;
echo "admin_view.php?edit_category=$table&page=$prev";?>
">Previous</a></li>
<?php
if($page != ceil($total_rows/$results_per_page)-1)
$next = $page+2;
else
$next = $page+1;
if($start_index != 1)
echo "<li class=\"page-item\"><a class=\"page-link\" href=\"admin_view.php?edit_category=$table&page=1\">1</a></li><li class=\"page-item\"><a class=\"page-link\">...</a></li>";
for ($x = $start_index; $x <= $end_index; $x++) {
echo "<li class=\"page-item\"><a class=\"page-link\" href=\"admin_view.php?edit_category=$table&page=$x\">$x</a></li>";
}?>
<?php
if($end_index != $last_page)
{
echo "<li class=\"page-item\"><a class=\"page-link\">...</a></li>
<li class=\"page-item\"><a class=\"page-link\" href=\"admin_view.php?edit_category=$table&page=$last_page\">$last_page</a></li>";
}
?>
<li class="page-item"><a class="page-link" href="<?php echo "admin_view.php?edit_category=$table&page=$next";?> ">Next</a></li>
</ul>
</nav>
<?php } ?>
</div>
</div>
</div>
<?php
include 'templates/footer.php';
?>