ketto
ketto

Reputation: 21

Math operation in PHP & MySQL

I'm creating a table using a PHP from the MySQL query which return a total count of rows from two columns in the database, "total_tr" and "total_rc".

I've already done and successfully view the count in the PHP table, the coding is:

 while($row = mysql_fetch_array($result))
      {
            echo "<tbody>";
              echo "<tr>";
                  echo "<td>Zone</td>";
                  echo "<td>" . $row['segment_code'] . "</td>";
                  echo "<td>" . $row['COUNT(total_tr)'] . "</td>";
                  echo "<td>" . $row['COUNT(repeat_rc)'] . "</td>";
              echo "</tr>";
            echo  "</tbody>"; 
      }  

My problem now is, I want to take the total count value of "total_tr", divided with total count of "repeat_rc" and multiply with 100 to get the percentage of total_rc.

Any ideas on how can I do that?

Upvotes: 2

Views: 1055

Answers (2)

machineaddict
machineaddict

Reputation: 3236

$myresult = $row['COUNT(total_tr)'] / $row['COUNT(repeat_rc)'] * 100;

Upvotes: 1

Marc B
Marc B

Reputation: 360862

Keep a running count as you echo the rows

$total_tr = 0;
$total_rc = 0;
while($row = ...) {
    $total_tr += $row['COUNT(total_tr)']);
    $total_rc += $row['COUNT(repeat_rc)']);
    ... html here ...
}
echo $total_tr / $total_rc * 100;

Upvotes: 0

Related Questions