-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_export.php
61 lines (46 loc) · 1.42 KB
/
c_export.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
<?php
// export.php
session_start();
include 'db/db_connect.php'; // Your database connection
if (!isset($_SESSION['loggedin'])) {
header('Location: index.php');
exit;
}
if ($_SESSION['type'] != 'coach') {
header('Location: index.php');
exit;
}
$user_id = $_POST['player_id'];
$conn = $con;
$stmt = $conn->prepare("SELECT * FROM players WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$res = $stmt->get_result();
$player_info = $res->fetch_assoc();
if ($player_info["coach_id"] != $_SESSION['coach_id']) {
exit('No Acess!');
}
$name = $player_info['player_name'];
// Query to fetch the user's data
$sql = "SELECT * FROM shots WHERE player_id = ? ORDER BY shot_date";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
// Output CSV headers
header('Content-Type: text/csv; charset=utf-8');
$filename = 'Content-Disposition: attachment; filename=';
$filename .= $name .'_shotstreak.csv';
header($filename);
// Open output stream
$output = fopen('php://output', 'w');
// Output column headings
fputcsv($output, ['Date', 'Shots Taken', 'Shots Made', 'Shooting Percentage']);
// Output user data rows
while ($row = $result->fetch_assoc()) {
$percentage = ($row['shots_made'] / $row['shots_taken']) * 100;
fputcsv($output, [$row['shot_date'], $row['shots_taken'], $row['shots_made'], round($percentage, 2) . '%']);
}
fclose($output);
$stmt->close();
?>