-
Notifications
You must be signed in to change notification settings - Fork 0
/
view_students.php
189 lines (130 loc) · 5.29 KB
/
view_students.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
<?php # view_students.php - N. Nasteff
/* This page displays all the currently registered students in the database.
Admins are given the option to filter the users by class number, as well as
delete a record.*/
$page_title = "Admin";
$page_header = "View Students";
include('includes/header.html');
echo '<br><div class="container" padding="10px">
<div class="panel panel-default">';
// Define string for filtering by class
$admin_string = 'view_students.php?class=';
// Number of results to display per page..
$display = 10;
// Determine class sort number..
$class_sort = FALSE;
if (isset($_SESSION['admin_id'])){
if (isset($_GET['class']) && is_numeric($_GET['class'])){
$class_sort = (int)$_GET['class'];
}
// If page numbers have already been assigned..
if (isset($_GET['p']) && is_numeric($_GET['p'])) {
$pages = $_GET['p'];
}
// Else, determine number of pages
else {
// Count the number of records. If class filter is applied, limit to appropriate class number.
if ($class_sort){
$count_query = "SELECT COUNT(student_id) FROM students WHERE class_num=$class_sort";
}
else {
$count_query = "SELECT COUNT(student_id) FROM students";
}
$result = @mysqli_query ($db_conn, $count_query);
$row = @mysqli_fetch_array ($result, MYSQLI_NUM);
$drive_count = $row[0];
// Calculate the number of pages...
if ($drive_count > $display) { // More than 1 page.
$pages = ceil ($drive_count/$display);
} else {
$pages = 1;
}
} // End of p IF.
// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
// Define the query (checks if results are to be sorted by ITN class)
if ($class_sort){
$student_query = "SELECT * FROM students WHERE class_num=$class_sort ORDER BY email ASC LIMIT $start, $display";
}
else {
$student_query = "SELECT * FROM students ORDER BY email ASC LIMIT $start, $display";
}
// Execute query
$result = mysqli_query ($db_conn, $student_query);
// Create a dropdown for a class filter (to filter the results by ITN class number)..
echo 'Filter by class: <select onChange="window.location.href=this.value"><option value="view_students.php">None</option>';
foreach (ITN_CLASSES as $classnum){
// Populate the dropdown options. If results are sorted by class number,
// that option gets the selected tag.
if ($classnum == $class_sort){
echo '<option value="'.$admin_string, $classnum.'" selected>'.$classnum.'</option>';
}
else {
echo '<option value="'.$admin_string, $classnum.'">'.$classnum.'</option>';
}
}
echo '</select>';
// Table header:
echo '<br><table cellpadding="10" class="table-striped" align="center">
<center><th>Name/Group</th><th><center>Email</th><th>Class Number</th><th>
Register Date</th><th><center>Active?</th><th></th>';
// Fetch and print all the records....
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr><td>'.$row['name'].'</td><td>'.$row['email'].'</td><td>'.$row['class_num'].'</td><td>'.$row['reg_date'].'</td><td>'.boolean_format($row['active']).'</td><td><a href="delete_student.php?sid='.$row['student_id'].'"">Delete</a>';
} // End of WHILE loop.
echo '</table>';
// Create back button..
echo '<br><button class="btn-dark" type="button" name="back" onclick="document.location.href=\'admin.php\'">Back to admin panel</button>';
mysqli_free_result ($result);
mysqli_close($db_conn);
// Make the links to other pages, if necessary.
if ($pages > 1) {
echo '<br><p class="blockquote">';
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous button:
if ($current_page != 1) {
if ($class_sort){
echo '<a href="view_students.php?s=' . ($start - $display) . '&p=' . $pages . '&class='. $class_sort . '"><</a> ';
}
else {
echo '<a href="view_students.php?s=' . ($start - $display) . '&p=' . $pages . '"><</a> ';
}
}
// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
if ($class_sort){
echo '<a href="view_students.php?s=' . (($display * ($i - 1))) . '&p=' . $pages .'&class=' .$class_sort.'">' . $i . ' </a> ';
}
else {
echo '<a href="view_students.php?s=' . (($display * ($i - 1))) . '&p=' . $pages .'">' . $i . '</a> ';
}
} else {
echo $i . ' ';
}
} // End of FOR loop.
// If it's not the last page, make a Next button:
if ($current_page != $pages) {
if ($class_sort){
echo '<a href="view_students.php?s=' . ($start + $display) . '&p=' . $pages . '&class='.$class_sort.'">></a>';
}
else {
echo '<a href="view_students.php?s=' . ($start + $display) . '&p=' . $pages . '">></a>';
}
}
echo '</p>'; // Close the paragraph.
// Back button..
}
}
else {
$url = BASE_URL .'admin_login.php';
ob_end_clean();
header("Location: $url");
exit();
}
include ('includes/footer.html');
?>