First commit
[westcastle.git] / scripts / rw_players.php
1 <?php
2
3 if (!isset($_POST["players"]))
4 {
5 // Retrieve all players
6 $handle = fopen("../joueurs.csv", "r");
7 $players = [];
8 $row = 0;
9 $data = fgetcsv($handle); //skip header
10 while (($data = fgetcsv($handle)) !== FALSE)
11 {
12 $players[$row] = array(
13 "prenom" => $data[0],
14 "nom" => $data[1],
15 "score" => count($data)>=3 ? $data[2] : 0,
16 "pdt" => count($data)>=4 ? $data[3] : 0,
17 "available" => count($data)>=5 ? $data[4] : 1,
18 );
19 $row++;
20 }
21 fclose($handle);
22 echo json_encode($players);
23 }
24 else
25 {
26 // Write header + all players
27 $handle = fopen("../joueurs.csv", "w");
28 fputcsv($handle, ["prenom","nom","score","pdt","present"]);
29 $players = json_decode($_POST["players"]);
30 foreach ($players as $p)
31 fputcsv($handle, (array)$p);
32 fclose($handle);
33 }
34
35 ?>