From 6c5adf546cd871d67e8b1f83c67d980b92d8d5d4 Mon Sep 17 00:00:00 2001 From: Trygve Date: Tue, 8 Oct 2024 21:40:46 +0200 Subject: [PATCH] La til sortering --- get_table.php | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/get_table.php b/get_table.php index 251733f..b379291 100644 --- a/get_table.php +++ b/get_table.php @@ -29,6 +29,17 @@ class Runner { $this->splits[$control] = $timestamp; } + + function get_control() + { + // Returns wich control the runner last passed + for ($i = 0; $i < count($this->splits); $i++) { + if (!is_object($this->splits[$i])) { + return $i-1; + } + } + return count($this->splits)-1; + } } //find runner by id in list of Runner objects @@ -42,6 +53,30 @@ function get_runner($runnner_list, int $id) return false; } +function cmp(Runner $a, Runner $b) { + $a_control = $a->get_control(); + $b_control = $b->get_control(); + if ($a_control > $b_control){ + return -1; + } + if ($a_control < $b_control){ + return 1; + } + if ($a_control == -1) { + return 1; + } + if ($b_control == -1) { + return -1; + } + if ($a->splits[$a_control]->getTimestamp() < $b->splits[$b_control]->getTimestamp()){ + return -1; + } + if ($a->splits[$a_control]->getTimestamp() > $b->splits[$b_control]->getTimestamp()){ + return 1; + } + return 0; +} + $runners = []; $csv_runners = file_get_contents("db.csv"); $csv_runners = str_getcsv($csv_runners, "\n"); @@ -72,7 +107,15 @@ for ($i = 0; $i < count($timings); $i++) { $runner->set_split($line[0]-1, $time); } -//print_r($runners); +usort($runners, "cmp"); +echo(" + # + Startnummer + Navn + 1. matpost + 2. matpost + Mål + "); for ($i = 0; $i < count($runners); $i++) { $runner = $runners[$i]; $tid_1_mat = ""; @@ -82,13 +125,11 @@ for ($i = 0; $i < count($runners); $i++) { } $tid_2_mat = ""; if ($runner->splits[1] != false) { - // https://www.php.net/manual/en/class.dateinterval.php $tid_2_mat = $GLOBALS['start_time']->diff($runner->splits[1])->format('%H:%I:%S'); } $tid_maal = ""; if ($runner->splits[2] != false) { - // https://www.php.net/manual/en/class.dateinterval.php $tid_maal = $GLOBALS['start_time']->diff($runner->splits[2])->format('%H:%I:%S'); } - echo ("$runner->id$runner->name$tid_1_mat$tid_2_mat$tid_maal"); + echo ("". $i+1 .".$runner->id$runner->name$tid_1_mat$tid_2_mat$tid_maal\n"); } \ No newline at end of file