Kevin Murphy
Kevin Murphy

Reputation: 408

Bold Largest Value - Comparing Values

I'm trying to compare two numbers, and add an HTML class to the large one- in this case statistics for a video game. Now I could do $player1[stat]-$player2[stat] and if the result is negative, player2 had a large value or just do normal greater than or less than logic. But, the thing is, I have about 20 different statistics I have to compare and add the class.

What would be the easiest way of making the larger value bold?

I was going to try case switches but that seems tedious. 20 different if-statements also seemed unnecessary.

Thanks!

Edit: Just to be clearer, I need to echo the values as well, but have the largest bolded

$player1[kills] = 55;  
$player2[kills] = 40;  

$player1[deaths] = 15;  
$player2[deaths] = 10;

The desired result:

<table>  
    <tr> 
        <th>Statistic</th>
        <th>Player 1</th>   
        <th>Player 2</th>        
    </tr>
    <tr> 
        <th>Kills</th>
        <th><strong>40Player 2</th>        
    </tr>  
    <tr> 
        <th>Deaths</th>
        <th>15</th>   
        <th><strong>10</strong></th>        
    </tr>      
</table>

Another thing- less deaths is better, so is there a way to some how chose the smaller one for that instance?

Upvotes: 1

Views: 140

Answers (2)

Starx
Starx

Reputation: 79031

When you are retrieving the value of kills and death, add them onto a global array.

$kills = array();
$death = array();
while($row = mysql_fetch_assoc($query)) //... or some database operation
    $kills[] = $row['kills'];
    $death[] = $row['deaths';

    //do other details
}

Now find out the maximum of each values

$killMax = max($kills);
$deathMax = max($death);

Now wherever you are display compair to this

echo $player1['kill']==$killMax ? "<strong>".$killMax."</strong>" : $killMax;

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270727

Retrieve the largest value from each group with something like:

$maxkills = max($player1['kills'], $player2['kills']);
$maxdeaths = max($player1['deaths'], $player2['deaths']);

On output:

if ($player1['kills'] == $maxkills) {
   // Add <strong> tag
   echo "<strong>{$player1['kills']}</strong>";
}
else {
   // output without <strong> tag
   echo $player1['kills'];
}

You can use a ternary operator to shorten this as well:

echo $player1['kills'] == $maxkills ? "<strong>{$player1['kills']}</strong>" : $player1['kills'];

By the way, please put quotes around your array keys. Although PHP parses them correctly (by assuming you meant to use strings instead of constants), it is issuing a notice for each time you do it even if those notices are just piling up in log files.

Upvotes: 1

Related Questions