-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_matches.php
132 lines (112 loc) · 4.97 KB
/
view_matches.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
<?php
include 'session_handler.php';
include 'config.php';
// Pagination setup
$limit = 8;
$page = isset($_GET['page']) ? max((int) $_GET['page'], 1) : 1;
$offset = ($page - 1) * $limit; // Offset calculation
// Query to get paginated records
$sql = "SELECT * FROM tournament_data ORDER BY date DESC LIMIT ? OFFSET ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $limit, $offset);
$stmt->execute();
$result = $stmt->get_result();
// Total records count for pagination
$total_sql = "SELECT COUNT(*) AS total FROM tournament_data";
$total_result = $conn->query($total_sql);
$total_row = $total_result->fetch_assoc();
$total_records_fetched = $total_row['total'];
$total_pages = ceil($total_records_fetched / $limit); // Total pages required
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tournament History</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300..800;1,300..800&display=swap"
rel="stylesheet">
<link rel="icon" href="./media/scorecard.com.png" type="image/png">
<link rel="stylesheet" href="css/view_matches.css">
</head>
<body>
<h2>Tournament History</h2>
<?php
if ($result->num_rows > 0) {
// Output table header
echo "<table>";
echo "<tr><th>Match No.</th><th>Date</th><th>Home</th><th>Away</th><th>Venue</th><th>Toss And Decision</th><th>Result</th><th>Actions</th></tr>";
// Calculate the starting match number for the current page
$start_match_number = $total_records_fetched - $offset;
// Start row number for the current page
$row_number = $start_match_number;
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row_number . "</td>"; // Display the match number
$formatted_date = date("F j, Y", strtotime($row['date']));
echo "<td>" . $formatted_date . "</td>";
echo "<td>" . $row['home_team'] . "</td>";
echo "<td>" . $row['away_team'] . "</td>";
echo "<td>" . $row['venue'] . "</td>";
echo "<td>" . $row['toss'] . " won the toss and decided to " . $row['decision'] . " first</td>";
echo "<td>" . $row['result'] . "</td>";
echo "<td class='action-buttons'>";
// Enable update button regardless of result value
echo "<button class='update-btn' data-match-no='" . $row['match_no'] . "' data-result='" . $row['result'] . "'>Update</button>";
echo "<button class='delete-btn' onclick='deleteMatch(" . $row['match_no'] . ")'>Delete</button>";
echo "</td>";
echo "</tr>";
$row_number--;
}
echo "</table>";
// Pagination controls
echo "<div class='pagination'>";
if ($page > 1) {
echo "<a href='?page=" . ($page - 1) . "' class='prev'>Previous</a>";
}
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='?page=" . $i . "' class='" . ($i == $page ? 'active' : '') . "'>" . $i . "</a>";
}
if ($page < $total_pages) {
echo "<a href='?page=" . ($page + 1) . "' class='next'>Next</a>";
}
echo "</div>";
} else {
echo '<div style="text-align: center; font-size: 25px;">No data found.</div>';
}
?>
<p><a href="create_match.php">Add New Match</a> | <a href="admin_dashboard.php">Go To Dashboard</a> | <a href="logout.php">Logout</a></p>
<script>
function updateMatch(matchNo, result) {
// Check if result is not empty
if (result !== "") {
// Redirect to edit_match.php if result is not empty
window.location.href = "edit_match.php?match_no=" + matchNo;
} else {
// Redirect to update_match.php if result is empty
window.location.href = "update_match.php?match_no=" + matchNo;
}
}
function deleteMatch(matchNo) {
// Display a confirmation dialog
if (confirm('Are you sure you want to delete this match?')) {
// If user confirms, redirect to delete page with the match number
window.location.href = "delete_match.php?match_no=" + matchNo;
}
}
// Add event listener to update buttons for extra caution, in case gets clicked through some external script or browser quirk
document.querySelectorAll('.update-btn').forEach(button => {
button.addEventListener('click', function (event) {
updateMatch(button.getAttribute('data-match-no'), button.getAttribute('data-result'));
});
});
</script>
<script>
function confirmLogout() {
return confirm("Are you sure you want to log out?");
}
</script>
</body>
</html>