-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_export.php
47 lines (37 loc) · 1.15 KB
/
p_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
<?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'] != 'player') {
header('Location: index.php');
exit;
}
$user_id = $_SESSION['player_id'];
$conn = $con;
// 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 .= $_SESSION['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();
?>