1

The following PHP script is pulling data from a MySQL database and displaying the results in a HTML table.

<?php

require_once 'config.php';

// create connection
$conn = new mysqli($currentConfig['host'], $currentConfig['user'], $currentConfig['pass'],
                   $currentConfig['name']);
// check connection
if ($conn->connect_error) {
  die('Connection failed: '.$conn->connect_error);
}

$sql = "SELECT Client, EstimateNumber, Status, TotalEstimatedTime, CostToDateRoleTotal, " .
       "ROUND((CostToDateRoleTotal / TotalEstimatedTime) * 100) AS PercentComplete " .
       "FROM Estimates " .
       "WHERE Status != 'Invoiced' AND Status != 'Cancelled' AND TotalEstimatedTime > 0 " .
       "AND CostToDateRoleTotal > 0 ORDER BY PercentComplete DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while ($row = $result->fetch_assoc()) {
    $client = $row['Client'];
    $estimate_number = $row['EstimateNumber'];
    $status = $row['Status'];
    $total_estimated_time = $row['TotalEstimatedTime'];
    $role_total = $row['CostToDateRoleTotal'];
    $percent_complete = $row['PercentComplete'];

    // echo data into HTML table
    echo '<tbody>'.'<tr>'.'<td>'.$client.'</td>'.'<td>'.$estimate_number.'</td>'.'<td>'.
         $status.'</td>'.'<td>'.$total_estimated_time.'</td>'.'<td>'.$role_total.'</td>'.
        '<td>'.$percent_complete.' %'.'</td>'.'</tr>'.'</tbody>';
  }
} else {
  echo 'No results';
}
$conn->close();

I want to add a <span> class to the <td> that outputs the $percent_complete values if the values in that cell are greater than 50. Both data types that are doing the calculation to create that row's data are DECIMAL. I tried to do the following:

  // echo flagged class to span element in table for percent complete
  if ( $percent_complete > 50 ) echo $flag = '<span class="flagged">';
  else echo '<span>';

And I edited the HTML table's echo statement to be:

  // echo data into HTML table
  echo
  '<tbody>'.'<tr>'.'<td>'.$client.'</td>'.'<td>'.$estimate_number.'</td>'.'<td>'.$status.'</td>'.'<td>'.$total_estimated_time.'</td>'.'<td>'.$role_total.'</td>'.'<td>'.$flag.$percent_complete.' %'.'</span>'.'</td>'.'</tr>'.'</tbody>';

However, this adds a <span class="flagged"> to every row. A var_dump() of $percent_complete gives me:

string(4) "1838"
string(4) "1591"
string(3) "592"
string(3) "416"
string(3) "367"
string(3) "346"
string(3) "305"
string(3) "267"
string(3) "266"
string(3) "231"
string(3) "193"
string(3) "169"
string(3) "157"
string(3) "149"
string(3) "142"
string(3) "136"
string(3) "134"
string(3) "127"
string(3) "114"
string(3) "109"
string(3) "106"
string(3) "103"
string(2) "96"
string(2) "88"
string(2) "71"
string(2) "71"
string(2) "70"
string(2) "67"
string(2) "59"
string(2) "58"
string(2) "46"
string(2) "44"
string(2) "38"
string(2) "38"
string(2) "37"
string(2) "34"
string(2) "18"
string(2) "16"
string(1) "9"
string(1) "4""

These are the string values of all of the rows in my table. How do I change my previous if statement to test against each of these string values separately? Thank you for your help.

1 Answer 1

1

Your logic doesn't hold, because you define the $flag variable once if $percent_complete > 50, but never define it back to an empty string when it's not. So its value is always <span class="flagged"> in every subsequent iteration thereafter.

This code

if ( $percent_complete > 50 ) echo $flag = '<span class="flagged">';
else echo '<span>';

should be

if ( $percent_complete > 50 ) {
    $flag = '<span class="flagged">';
} else {
    $flag = '<span>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly. You have a small typo - $flat should be $flag. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.