-
Notifications
You must be signed in to change notification settings - Fork 0
/
005.php
148 lines (133 loc) · 4.24 KB
/
005.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>第五週-方法及陣列</title>
<link href="https://fonts.googleapis.com/css?family=Noto+Sans+TC&display=swap" rel="stylesheet">
</head>
<style>
* {
margin: 0;
padding: 0;
}
body {
justify-content: content;
align-items: center;
height: 100vh;
display: flex;
background-color: #2d4654;
font-family: 'Noto Sans TC', sans-serif;
color: #fff;
}
.container {
width: 600px;
margin: 0 auto;
padding: 20px;
border-radius: 5px;
gap: 10px;
display: grid;
background-color: #243b4a;
box-shadow: #243b4a 0px 0px 50px;
}
.scarch {
display: flex;
flex-direction: column;
gap: 10px;
}
form {
display: flex;
flex-direction: row;
gap: 10px;
}
.result{
display: flex;
flex-direction: column;
gap: 10px;
}
form input[type="text"] {
padding: 5px;
border: none;
border-radius: 10px;
height: 20px;
background-color: #4e4d5c;
color: #fff;
width: 100%;
}
form input[type="submit"] {
background-color: #805e73;
border: none;
color: #fff;
font-weight: bold;
cursor: pointer;
padding: 6px 20px;
height: auto;
border-radius: 10px;
}
form input[type="submit"]:hover {
background-color: #4e4d5c;
}
</style>
<body>
<div class="container">
<?php
// declare a 2d array with 4 rows and 4 columns
$array = array(
array("王一", 90, 100, 80),
array("張二", 82, 85, 90),
array("Ray", 80, 65, 90),
array("KiKi", 60, 85, 80)
);
// declear a function to search the student's name
function search($name)
{
global $array;
// loop through the array
for ($i = 0; $i < count($array); $i++) {
// if the name is found
if ($array[$i][0] == $name) {
// return each scores
return $array[$i][1] . "," . $array[$i][2] . "," . $array[$i][3];
}
}
// if the name is not found, return -1
return -1;
}
?>
<!-- search bar for searching scores -->
<div class="scarch">
<form method="GET">
<input type="text" name="name" placeholder="請輸入學生姓名">
<input type="submit" name="submit" value="查詢">
</form>
<dev class="result">
<?php
// if the form is submitted
if (isset($_GET["submit"]) && $_GET["name"] != "") {
// get the name from the form
$name = $_GET["name"];
// call the function
$score = search($name);
// if the name is found
if ($score != -1) {
// display the each scores
$score_str = explode(",", $score);
echo "<p>姓名:" . $name . "</p>";
echo "<p>國文:" . $score_str[0] . "</p>";
echo "<p>英文:" . $score_str[1] . "</p>";
echo "<p>數學:" . $score_str[2] . "</p>";
echo "<p>總分:" . ($score_str[0] + $score_str[1] + $score_str[2]) . "</p>";
echo "<p>平均:" . round(($score_str[0] + $score_str[1] + $score_str[2]) / 3, 2) . "</p>";
}
else {
// display the error message
echo "<p> 姓名:$name <br> !學生不存在!</p>";
}
}else{
echo "<p>請輸入學生姓名</p>";
}
?>
</dev>
</div>
</div>
</body>
</html>