0

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++;
}
2
  • You should show it with more decimal places to have accurate data. Commented Jun 20, 2018 at 12:37
  • if you have failure but less than 0.01% you can just round the number to two decimals so that it will give you 0.01 when you have 0.001. So before applying number format to val round it up Commented Jun 20, 2018 at 12:37

2 Answers 2

1

What about checking $val before it's formatted, and then signifying whether it's "0.01%" or "<0.01%"?

if($colindex == 4){
    if($val < 0.01) {
        $valStr = "<0.01%";
    } else {
        $valStr = number_format($val, 2, '.', '');
    }
    echo "<td $style>$valStr</td>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

As long as I factor in the fact if there is failure before, this solves the issue. thanks
1

just round your number. So change your code like this:

        foreach( $cols as $column_name ) {
            $style = "";
            $val = $row[$column_name];
            //this will give you the number rounded to two digits
            $val = round($val,2);

            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++;
        }

You were applying directly number_format that would cut just the first two digits. So if you have 0.001 number format will cut to 0.00 while round will give 0.01 with two digits of rounding

Comments

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.