Jump to content

position script


darbyp

Recommended Posts

hi, i have finally got my database set up for club timetrial results.

 

I have got the first round results being pulled out of the database like this

array(6) {
  ["round_num"]=>
  string(1) "1"
  ["round_date"]=>
  string(10) "2015-04-15"
  ["rider_firstname"]=>
  string(4) "firstname"
  ["rider_lastname"]=>
  string(10) "lastname"
  ["club_name"]=>
  string(14) "clubname"
  ["rider_time"]=>
  string( "00:22:03"
}
array(6) {
  ["round_num"]=>
  string(1) "1"
  ["round_date"]=>
  string(10) "2015-04-15"
  ["rider_firstname"]=>
  string(4) "firstname"
  ["rider_lastname"]=>
  string(7) "lastname"
  ["club_name"]=>
  string(9) "clubname"
  ["rider_time"]=>
  string( "00:22:11"
}
array(6) {
  ["round_num"]=>
  string(1) "1"
  ["round_date"]=>
  string(10) "2015-04-15"
  ["rider_firstname"]=>
  string(5) "firstname"
  ["rider_lastname"]=>
  string(7) "lastname"
  ["club_name"]=>
  string(14) "clubname"
  ["rider_time"]=>
  string( "00:22:21"
}

from a while($result = mysqli_fetch_assoc($query)) (if that is the best way of doing it)

 

Is there anyway of looping through them and assigning a position i.e. 1, 2 ,3, 4 etc and account for people who finish with the same time so will have to be the same number but then skip a few to get back on track?

 

Hope it makes sense

 

Thanks

Edited by darbyp
Link to comment
Share on other sites

You need keep track of the previous score and maintain a count. EG

$results = array(
                array ('name'=>'Peter', 'time'=>'00:20:30'),
                array ('name'=>'Paul', 'time'=>'00:22:12'),
                array ('name'=>'Ted', 'time'=>'00:21:42'),
                array ('name'=>'Ben', 'time'=>'00:23:04'),
                array ('name'=>'Tom', 'time'=>'00:21:42'),
                array ('name'=>'Hank', 'time'=>'00:24:36'),
                array ('name'=>'Fred', 'time'=>'00:21:42')
                );
// sort the results by time
uasort($results, function($a, $b) { return strcmp($a['time'], $b['time']); });

// display results showing the ranking positions

$prevtime = '00:00:00';
$count = 0;
$pos = 0;
foreach ($results as $rider) {
    ++$count;
    $pos = ($rider['time']==$prevtime) ? $pos : $count;
    $prevtime = $rider['time'];
    echo "$pos : {$rider['name']} ({$rider['time']})<br>";
}

/* output ******************

1 : Peter (00:20:30)
2 : Tom (00:21:42)
2 : Ted (00:21:42)
2 : Fred (00:21:42)
5 : Paul (00:22:12)
6 : Ben (00:23:04)
7 : Hank (00:24:36)

*/
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.