-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolus_listCou.php
More file actions
158 lines (151 loc) · 5.75 KB
/
Copy pathsolus_listCou.php
File metadata and controls
158 lines (151 loc) · 5.75 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
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
<!-- // Purpose: Display all courses and search for courses
// Author: Songyu Yang -->
<?php
session_start();
// Display data
include 'solus_connectdb.php';
$pageSize = 3;
$where = '';
if(isset($_GET['submit'])) {
// The first step in paging: calculate the total number of records
$text = $_GET['search'];
$sql = "select count(*) as num from course where code = '$text'";
}else{
$sql = "select count(*) as num from course ";
}
// The second step in paging: calculate the total number of records
$res = $connection->query($sql);
$count = $res->fetch(PDO::FETCH_ASSOC);
$number = $count['num'];
$class = '';
// The third step in paging: calculate the total number of pages
if ($_SESSION['role'] == 1) {
$class = "hide";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Course</title>
<link rel="stylesheet" type = "text/css" href="main.css">
<link rel="stylesheet" type = "text/css" href="Table_CSS.css">
</head>
<style>
.box {
width: 100%;
height: auto;
display: flex;
justify-content: center;
}
.child {
background: ;
padding: 20px;
}
.table tr th{
padding: 5px;
}
.table tr td{
padding: 5px;
}
.pagination li{
width: 15px;
}
.btn{
padding: 2px 6px;
background: #2daecf;
text-decoration: none;
color: #ffffff;
}
.hide {
display: none!important;
}
</style>
<body style="background: ">
<div class="box">
<div class="child">
<form method="GET" action="solus_listCou.php">
Please Input Course Code:<input type="text" name="search" value="<?=isset($_GET['search'])?$_GET['search']:''?>" >
<input type="submit" name="submit" value="search">
<a href='index.php'>Home</a>
</form>
</div>
</div>
<div class="box">
<div class="child">
<table border="1" bordercolor="a0c6e5" style="border-collapse: collapse;" class="table <?php if (!$number || !isset($text)) {echo 'hide';} ?>">
<tr>
<th>code</th>
<th>units</th>
<th>learningHours</th>
<th>description</th>
<th class="<?php echo $class;?>">Operate</th>
<tr>
<?php
// The third step in paging: Get the current page
$page = isset($_GET['p']) ? $_GET['p'] : 1;
// The fourth step in paging: Calculate the offset = (current page -1) * Number of bars displayed per page
$pageLimit = ($page - 1) * $pageSize;
// SQL statement
$sql = "select * from course limit $pageLimit,$pageSize";
$bookQ = $connection->query($sql)->fetchAll(PDO::FETCH_ASSOC);
// Calculate the number of pages: ceil(total records/number of pages displayed)
$pageNum = ceil($number / $pageSize);
// Calculate the previous page number = current page -1, but not less than 1
$upPage = $page - 1 < 1 ? 1 : $page - 1;
// Calculate the page number of the next page = current page +1, but less than the total number of pages
$nextPage = $page + 1 > $pageNum ? $pageNum : $page + 1;
if(isset($_GET['submit'])) {
// Search
$searchTotal = $connection->query("SELECT count(*) FROM `course` where `code` = '$text'")->fetch(PDO::FETCH_ASSOC);
if ($searchTotal['count(*)'] == 0) {
// if the search result is empty, prompt the user and return to the previous page
echo "<script>alert('Not found!');location.href='solus_enterCou.php'</script>";
exit;
}
// search the specified course
$searchQ = $connection->query("SELECT * FROM `course` where `code` = '$text'")->fetchAll(PDO::FETCH_ASSOC);
foreach($searchQ as $search) {
echo "<tr>
<td align='center'>".$search['code']."</td>
<td align='center'>".$search['units']."</td>
<td align='center'>".$search['learningHours']."</td>
<td align='center'>".$search['description']."</td>
<td align='center' class='{$class}'> <a class='btn' href='solus_editCou.php?id={$search['code']}'>edit</a> | <a class='btn' href='solus_enterCou.php'>add</a></td>
</tr>";
}
} else {
// Display all courses
foreach($bookQ as $book) {
echo "<tr>
<td align='center'>".$book['code']."</td>
<td align='center'>".$book['units']."</td>
<td align='center'>".$book['learningHours']."</td>
<td align='center'>".$book['description']."</td>
<td align='center' class='{$class}'> <a class='btn' href='solus_editCou.php?id={$book['code']}'>edit</a> | <a class='btn' href='solus_enterCou.php'>add</a></td>
</tr>";
}
}
?>
</table>
<ul class="pagination <?php if (!$number || !isset($text)) {echo 'hide';} ?>" style="display: flex;list-style: none">
<!-- previous-->
<li style="width: 15px"><a href="solus_listCou.php?p=<?php echo $upPage?>">«</a></li>
<?php
// The fifth step in paging: Display the page number
for ($i = 1; $i <= $pageNum; $i++) {
// The sixth step in paging: Highlight the current page
?>
<li><a href="solus_listCou.php?p=<?php echo $i ?>"><?php echo $i ?></a></li>
<?php
}
?>
<!-- next page-->
<li><a href="solus_listCou.php?p=<?php echo $nextPage?>">»</a></li>
</ul>
</div>
</div>
</body>
</html>