• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Variable not available to another variable?

MattW

Well-known member
#1
I'm currently re-writing a monitoring tool I wrote at work a few years ago.

For some reason, one of the variables I'm setting isn't available when I try to use it to set another variable (only seems to work when I echo it out).

Here is the code in question
PHP:
while ($row = $totalCallsToday->fetch_assoc()) {
        if (!is_null($row['dial'])) {
                echo "<tr><td>".$row['dial']."</td><td>".number_format($row['count'])."</td><td>".$row['percentage']."%</td></tr>";
        }
        if (is_null($row['dial'])) {
                $total = number_format($row['count']);
        }
}   
$clear_percent = number_format($numClearsToday->num_rows / $total * 100, 2);
echo "<tr><td>Total Calls - ".$total."</td><td>Modem Clears - ".$numClearsToday->num_rows." ( $clear_percent% )"; ?>
So what I'm doing, it setting the $total variable in the while loop, as that is the result of the WITH ROLLUP from the SQL statement (this is the total number of calls).

I've then got another set of results which I want to use the value of $total to calculate the percentage of "Modem Clears" in this variable

PHP:
$clear_percent = number_format($numClearsToday->num_rows / $total * 100, 2);
This is what it's outputting the results like

upload_2013-10-12_23-0-52.png

If I replace the $total in the $clear_percent variable with a static figure, I get a calculated %, but when I use the $total variable, it's not working.

Any idea why this wouldn't be available (when I can actually echo the value of $total out)?
 

MattW

Well-known member
#2
Managed to figure it.

WITH ROLLUP gives the result back as a string.

PHP:
var_dump($total);
string(4) "1234"

Convert it to intval
PHP:
$total = intval($row['count']);
and it works
upload_2013-10-12_23-27-8.png