I am trying to create a table of data with a large number of Runs/Passes/Fails. When there is a high number of runs and only a select few fails, the Fail% usually goes to 0.00% instead of the actual percent. I figured that I should display the 0.01% to show that there is SOME failure in the case. I have posted the code below that creates my table, and an example of the table desired.
My Desired Table
Name Runs Pass Fail Fail %
Tester_A 1,000,000 1,000,000 0 0.00%
Tester_B 1,000,000 800,000 200,000 20.00%
Tester_C 1,000,000 999,985 15 0.01% Notice 15/10000000 should not be 0.01%
My Current Table
Name Runs Pass Fail Fail %
Tester_A 1,000,000 1,000,000 0 0.00%
Tester_B 1,000,000 800,000 200,000 20.00%
Tester_C 1,000,000 999,985 15 0.00%
The code I use to create my table:
<table>
<thead>
<tr>
<?php
// removed the hard coded column headers and took the ones from our query
global $hcols;
foreach($hcols as $column_header) {
echo "<th>$column_header</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
//Use queried data to create each row of the table
$rowcount=0;
global $db_query;
global $column_callback;
if ( isset($db_query)) {
while($row = mysqli_fetch_array($db_query)) {
$rowcount++;
// removed the hard coded column set and made it driven off of the array below
echo "<tr>";
$colindex = 0;
foreach( $cols as $column_name ) {
$style = "";
$val = $row[$column_name];
if ( isset($column_callback)) {
$style=$column_callback($colindex, $val);
}
if($colindex == 4){ // formats decimal to 0.00%
$val = number_format($val, 2, '.', '');
echo "<td $style>$val</td>";
} else {
echo "<td $style>$val</td>";
}
$colindex++;
}
echo "</tr>\n";
}
}
?>
</tbody>
</table>
Something I have tried, did not display the desired output, just the original of the 0.00 instead of the 0.01: In foreach I tried storing a few temp values..
foreach( $cols as $column_name ) {
$style = "";
$val = $row[$column_name];
if ( isset($column_callback)) {
$style=$column_callback($colindex, $val);
}
if($colindex == 3){
$temp_fail = $val; // store # of fails from this row
echo "<td $style>$val</td>";
}
if($colindex == 4){
$val = number_format($val, 2, '.', ''); // formats decimal to 0.00%
if($temp_fail != 0 && $val == 0.00){ // check if number of fails is > 0
$val = $val + 0.01; // add that 0.01 to show SOME failure
echo "<td $style>$val%</td>";
} else {
echo "<td $style>$val%</td>";
}
} else {
echo "<td $style>$val</td>";
}
$colindex++;
}