waflfootyfacts
waflfootyfacts

Reputation: 61

Bold the higher value in row

I have a table with three columns, 1. Home teams score, 2. "Points Scored" and 3. Away teams score and would like to make the higher value display in bold whilst the lowest value remains normal.

I an get the results to return but not sure how to make the higher value only be bold.

<tr>
  <td width="39%" colspan="3" align="right"><?php  
    $result = mysqli_query($con,"SELECT Total
                            FROM MatchDetails2017
                            WHERE GameID = $GameID AND HA='H'");
    $numrows = mysqli_num_rows($result);
    if($numrows > 0) {          
    while($row = mysqli_fetch_array($result)) {
      echo "<strong>".$row['Total'."</strong>"];        
     }  
    }

?></td> 
  <td width="22%" align="center"><strong>Points Scored</strong><br/></td>
  <td width="39%" colspan="3" align="left"><?php  
    $result = mysqli_query($con,"SELECT Total
                            FROM MatchDetails2017
                            WHERE GameID = $GameID AND HA='A'");
    $numrows = mysqli_num_rows($result);
    if($numrows > 0) {          
    while($row = mysqli_fetch_array($result)) {
      echo "<strong>".$row['Total']."</strong>";        
     }  
    }

?></td> 
</tr>

Upvotes: 1

Views: 114

Answers (1)

The Codesee
The Codesee

Reputation: 3781

This code stores the home team scores and away team scores in seperate arrays. It then loops through each element in the $homeTeamScores and gets the corresponding element in the $awayTeamScores. It then compares the values and makes the higher one bold.

I've used PDO instead of MySQL. This is important because it helps protect your database against SQL injections.

<?php
$homeTeamScores = array();
$awayTeamScores = array();
// Get home teams scores 
$stmt = $con->prepare("SELECT total FROM MatchDetails2017 WHERE GameID=:gameID AND HA=:H");
$stmt->bindParam(':gameID', $GameID);
$stmt->bindValue(':H', 'H');
$stmt->execute();
if ($stmt->rowCount() > 0) {
   while ($row = $stmt->fetch()) {
      array_push($homeTeamScores, $row['Total']);
   }
}
// Get away teams scores
$stmt = $con->prepare("SELECT total FROM MatchDetails2017 WHERE GameID=:gameID AND HA=:A");
$stmt->bindParam(':gameID', $GameID);
$stmt->bindValue(':A', 'A');
$stmt->execute();
if ($stmt->rowCount() > 0) {
   while ($row = $stmt->fetch()) {
      array_push($awayTeamScores, $row['Total']);
   }
}

// Now loop through the values in the arrays and display them in the table
$i = 0;
foreach ($homeTeamScores as $homeTeamScore) {
   $awayTeamScore = $awayTeamScores[$i];
   echo '
       <tr>
       <td width="39%" colspan="3" align="right">
   ';
   if ($awayTeamScore < $homeTeamScore) { // home team score is bigger than home team score
      echo '<strong>'.$homeTeamScore.'</strong>';
   } else {
      echo $homeTeamScore;
   }
   echo '
      </td> 
      <td width="22%" align="center"><strong>Points Scored</strong></td>
      <td width="39%" colspan="3" align="left">
   ';
   if ($awayTeamScore > $homeTeamScore) { // awayteam score is bigger than away team score
      echo '<strong>'.$awayTeamScore.'</strong>';
   } else {
      echo $awayTeamScore;
   }
   echo '
       </td> 
       </tr>
   ';
   $i++;
}
?>

Upvotes: 1

Related Questions