php - Recognizing duplicates while iterating through query results -


this follow-up question asked previously.

i can calculate rank (including logic ties) fine; issue detecting future duplicates when come across first instance of duplicate rank.

here sql query result set:

select     s1.team_id,     sum(s1.score>s2.score) wins scoreboard s1     left join scoreboard s2         on s1.year=s2.year         , s1.week=s2.week         , s1.playoffs=s2.playoffs         , s1.game_id=s2.game_id         , s1.location<>s2.location group s1.team_id order wins desc; 

here sample sql result set i'll loop through in php:

team_id   wins -------------- 10        52 2         48 5         46 11        46 3         42 9         39 ... 

here php code display, needs append "t-" tied ranks:

$i = 0; while($row = mysql_fetch_assoc($r)) { //iterate thru ordered (desc) sql results     ++$i;     ($row['wins'] == $prev_val)         ? $rnk = 't-' . $rnk    //same previous score, indicate tie         : $rnk = $i;            //not same previous score     $rnk = str_replace('t-t-','t-',$rnk); //eliminate duplicative tie indicator     if ($row['team_id'] == $team_id) { //current team in resultset matches team in question, set team's rank         $arr_ranks['tp']['cat'] = 'total wins';         $arr_ranks['tp']['actual'] = number_format($row['wins'],1);         $arr_ranks['tp']['league_rank'] = $rnk;         $arr_ranks['tp']['div_rank'] = $div_rnk;     }     else if ($i == 1) { //current team category leader (rank=1) , not team in question, set current team leader         $arr_ranks['tp']['leader'] = "<a href='index.php?view=franchise&team_id=" . $row['team_id'] . "'>" . get_team_name($row['team_id']) . '</a> (' . number_format($row['wins']) . ')';     }     $prev_val = $row['wins']; //set current score previous score next iteration of loop } 

the "tie" logic above capture team #4 having tied team #3, not vice versa.

in other words, team #3, $rnk = 3, while team #4, $rnk = t-3. (both should "t-3".)

so question becomes: how "look ahead" while iterating through results find out if current score tie/duplicate of scores further down list, can treat tie along subsequent dupes?

@airzooka gave me a potential solution, i'm curious know if there's more efficient way (possibly @ sql level even).

thanks.

in pseudo-code:

loop through rows row1     loop through rows row2         if row1 ain't row2 , row1.points == row2.points, append t     

update:

ok, how this, since you're ordering result set wins, anyhow: try storing information each row in temporary array or variables, $previousteamwins, $previousteamname, etc. can compare current , previous , assign t based on that. you're delaying assignment until following iteration (or until exit of loop in case of final row). 1 trip through row set, should job done.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -