Divide if equal?!?

Discussion in 'PHP' started by jmansa, Mar 24, 2009.

  1. #1
    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:
    
    $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++;
    				 
    				
    			}
    PHP:
    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 :)
     
    jmansa, Mar 24, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Mostly I don't understand your question.

    However, check into functions floor(), ceil(), and round().
     
    SmallPotatoes, Mar 24, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    There's basically two ways to accomplish this. This first it to pre-query for the players with the most point and if there is a tie, adjust accordingly when you start looping through the results which are created during the second query.

    The second way is to loop through the result set 2 times, the first, finding the players with the most points, and the second displaying the results and calculations as needed.

    How many players are returned from a single query? If there aren't a lot (say 100 or less), looping twice will probably be the best way. If this is a huge result set, you will want to do 2 queries to save memory.
     
    jestep, Mar 24, 2009 IP
  4. jmansa

    jmansa Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not quite sure what you mean...Example ore link to tutorial would be nice... Thanks.
     
    jmansa, Mar 24, 2009 IP