-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_functions.php
143 lines (123 loc) · 3.31 KB
/
table_functions.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
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
switch($_POST["action"]) {
case "delete":
delete();
exit;
case "display":
displayTable();
exit;
case "addscore":
addScore();
exit;
case "subtractscore":
subtractScore();
exit;
default:
echo "No such function exists!";
exit;
}
}
function addScore() {
$conn = new mysqli("localhost", "root", "root", "myDB");
if($conn->connect_error) {
die("Connection failed: " . $conn->error);
}
$length = count($_POST);
if($_POST["scoreTeam"] == "home") {
for($i=0;$i<$length;$i++){
echo ($_POST[$i]);
$sql = "UPDATE sports SET Homescore = Homescore + 2 WHERE GameID=".$_POST["$i"]."";
$conn->query($sql);
}
}
if($_POST["scoreTeam"] == "away") {
for($i=0;$i<$length;$i++){
echo ($_POST[$i]);
$sql = "UPDATE sports SET Awayscore = Awayscore + 2 WHERE GameID=".$_POST["$i"]."";
$conn->query($sql);
}
}
$conn->close();
}
function subtractScore() {
$conn = new mysqli("localhost", "root", "root", "myDB");
if($conn->connect_error) {
die("Connection failed: " . $conn->error);
}
$length = count($_POST);
if($_POST["scoreTeam"] == "home") {
for($i=0;$i<$length;$i++){
echo ($_POST[$i]);
$sql = "UPDATE sports SET Homescore = Homescore - 2 WHERE GameID=".$_POST["$i"]."";
$conn->query($sql);
}
}
if($_POST["scoreTeam"] == "away") {
for($i=0;$i<$length;$i++){
echo ($_POST[$i]);
$sql = "UPDATE sports SET Awayscore = Awayscore - 2 WHERE GameID=".$_POST["$i"]."";
$conn->query($sql);
}
}
$conn->close();
}
function delete() {
$conn = new mysqli("localhost", "root", "root", "myDB");
if ($conn->connect_error) {
die("Connection failed: " . $conn->error);
}
$length = count($_POST);
for($i=0;$i<$length;$i++){
$sql = "DELETE FROM sports WHERE GameID=".$_POST["$i"]."";
$conn->query($sql);
}
$conn->close();
}
function displayTable() {
$conn = new mysqli("localhost", "root", "root", "myDB");
if ($conn->connect_error) {
die("Connection failed: " . $conn->error);
}
$sql = "SELECT * FROM sports";
$result = $conn->query($sql);
if ($conn->query($sql)) {
if ($result->num_rows > 0) {
echo "<table><tr>
<th>ID</th>
<th>Sport</th>
<th>Level</th>
<th>Date</th>
<th>Home</th>
<th>Away</th>
<th>Select</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["GameID"]."</td>
<td>".$row["Sport"]."</td>
<td>".$row["Level"]."</td>
<td>".$row["GameDate"]."</td>
<td>".$row["Homescore"]."</td>
<td>".$row["Awayscore"]."</td>
<td><input type='checkbox' name='game' value='".$row['GameID']."' id='".$row['GameID']."' onchange='setSelected(".$row["GameID"].", checked)'></td>
</tr>";
}
echo "</table>";
} else {
echo "No results";
}
} else {
echo "Error selecting: " . $conn->error;
}
}
?>
<!-- REFERENCES
ACTION METHODS:
delete: Deletes data from table.
display: Dislplays current table. To edit table, Go to displayTable function and edit <th> and <td> tags.
addscore: Adds score to either home or away.
subtractscore: Subtracts score from either home or away.
ATTRIBUTES:
scoreTeam: "home", "away"; Tells which team to add/subtract the points to/from.