darbyp Posted July 19, 2015 Share Posted July 19, 2015 (edited) 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 July 19, 2015 by darbyp Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 19, 2015 Share Posted July 19, 2015 (edited) Well, you could put an ORDER clause on your query. That way your results would already be ordered from least time -> most time, or vice versa. SELECT ... ORDER BY rider_time Edited July 19, 2015 by scootstah Quote Link to comment Share on other sites More sharing options...
Barand Posted July 19, 2015 Share Posted July 19, 2015 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) */ 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.