Page 1 of 1

Divide if equal?!?

Posted: Tue Mar 24, 2009 4:08 pm
by jmansa
I'm making this script where a tournament is played and a leaderboard is displayed. That part is OK, but... My trouble starts if 2 players are equal. Here is my script:

Code: Select all

 
$xtrapoints = array("6","3","2","0","0");
            
            echo '<table cellpadding="0" cellspacing="1">
                    <tr>
                        <td width="20" align="center" valign="middle" bgcolor="#6D84B4"><div style="padding:3px;color:#FFFFFF;">P</div></td>
                        <td width="150" align="left" valign="middle" bgcolor="#6D84B4"><div style="padding:3px;color:#FFFFFF;">Player</div></td>
                        <td width="50" align="center" valign="middle" bgcolor="#6D84B4"><div style="padding:3px;color:#FFFFFF;">Strokes</div></td>
                        <td width="50" align="center" valign="middle" bgcolor="#6D84B4"><div style="padding:3px;color:#FFFFFF;">StrokeNet</div></td>
                        <td width="50" align="center" valign="middle" bgcolor="#6D84B4"><div style="padding:3px;color:#FFFFFF;">Points</div></td>
                        <td width="50" align="center" valign="middle" bgcolor="#6D84B4"><div style="padding:3px;color:#FFFFFF;">Bonus</div></td>
                        <td width="50" align="center" valign="middle" bgcolor="#6D84B4"><div style="padding:3px;color:#FFFFFF;">Total</div></td>
                    </tr>
                        
                        
                        ';                              
            
            $sql="SELECT * FROM ".$prefix."_users 
                    INNER JOIN ".$prefix."_points ON ".$prefix."_users.new_userid = ".$prefix."_points.user 
                    INNER JOIN ".$prefix."_stroke ON ".$prefix."_users.new_userid = ".$prefix."_stroke.user
                    WHERE ".$prefix."_points.matchid=$matchid ORDER BY ".$prefix."_points.p_total DESC";
            $result = mysql_query($sql);
            
            $i = 1;
            $r_count = 0;
    
            while($row = mysql_fetch_array($result)){
        
                $fname = $row['fname'];
                $lname = $row['lname'];
                $tpoints = $row['p_total'];
                $tstroke1 = $row['stroke_total'];
                $tstroke2 = ($tstroke1 - $row['xtrastrokes']);
                
                $points_for_this_player = ($i < count($xtrapoints)) ? $xtrapoints[$i-1] : 0;
                $totalpoints = ($points_for_this_player + $tpoints);
                
                    
                 echo '<tr style="background-color:#' .((++$r_count %2 == 0) ? 'FFFFFF' : 'EEEEEE'). '">';
                 echo '<td width="" align="center" valign="middle">' . $i . '</td>';
                 echo '<td width="" align="left" valign="middle">&nbsp;'.$fname.' '.$lname.'</td>';
                 echo '<td width="" align="center" valign="middle">'.$tstroke1.'</td>';
                 echo '<td width="" align="center" valign="middle">'.$tstroke2.'</td>';
                 echo '<td width="" align="center" valign="middle">'.$tpoints.'</td>';
                 echo '<td width="" align="center" valign="middle">'.$points_for_this_player.'</td>';
                 echo '<td width="" align="center" valign="middle">'.$totalpoints.'</td>';
                 echo '</tr>';
                 
                 $i++;
                 
                
            }
As you can see the scripts generates a leaderboard with the player with most points ($tpoints) is listed first. The 3 top players is also given xtrapoints (6,3,1) for being the best, and it is here the trouble begins...

What if #1 and #2 player has the exact same amount of points!? Then the script should add the to bonus points (6+3=9) and divide them into 2. How do I do that.

9 divided in 2 gives 4,5 which also is a mess, so it should be set up to 5. Also if the number had been 4,1...

Hope this is understandable and hope for some help :-)

Re: Divide if equal?!?

Posted: Tue Mar 24, 2009 6:11 pm
by Ambush Commander
So there are two questions here: what do you want the code to actually do, and how to implement it. It sounds like you want to divide up the extra points if there are multiple people with the same point score: does that mean if three people have the same amount of points, they each get three points? What if there are nine people in first place and one person in second place: the second person now gets a lot of points, whereas the other people get one point?

Once you've decided on some logical behavior, we can talk about how to modify the code.