获得2个数字的百分比

I'm working with php and sql I made a table like this one belowe:

enter image description here

I'm getting all the data with mysql like you can see in the picture. Now I need to calculate the percentage of those 2 numbers for example 301 and 197 = %34.55 This i did in my head quickly but how can i make a method for this like in php.


I also did try this php code and this works great but it does not work in my table when I copy past it and change some code.

<?php

$row0 = 197;
$row1 = 301;

function percent($row0, $row1) {
    echo number_format((1 - $row0 / $row1) * 100, 2);
}

percent($row0, $row1);

This is the table I made:

echo '<table id="tablenl" class="table table-striped">';
echo '             <div class="container">'; 
echo '  <thead>';
echo '     <tr class="bg-primary">';
echo '    <th>Total Amount:</th>';
echo '    <th>Last Year</th>';
echo '    <th>YTD</th>';
echo '  </tr>';
echo '  <tr>';
echo '    <td>All countries: </td>';
echo '       <td><b>€ K'.$row0['total'].'</b></td>';
echo '       <td><b>€ K'.$row1['total'].'</b></td>';
echo '  </tr>';
echo '  <tr>';
echo '    <td>Netherlands: </td>';
echo '       <td><b>€ K'.$row2['total'].'</b></td>';
echo '       <td><b>€ K'.$row3['total'].'</b></td>';
echo '  </tr>';
echo '  <tr>';
echo '    <td>%: </td>';
echo '       <td>%</td>';
echo '       <td>%</td>';
echo '  </tr>';
echo '   </tbody>';
echo '</table>';
echo '</div>';

----------Those are real numbers coming from my database!!!

This way it seems to work fine:

echo '<table id="tablenl" class="table table-striped">';
echo '             <div class="container">'; 
echo '  <thead>';
echo '     <tr class="bg-primary">';
echo '    <th>Total Amount:</th>';
echo '    <th>Last Year</th>';
echo '    <th>YTD</th>';
echo '  </tr>';
echo '  <tr>';
echo '    <td>All countries: </td>';
echo '       <td><b>€ K'.$row0['total'].'</b></td>';
echo '       <td><b>€ K'.$row1['total'].'</b></td>';
echo '  </tr>';
echo '  <tr>';
echo '    <td>Netherlands: </td>';
echo '       <td><b>€ K'.$row2['total'].'</b></td>';
echo '       <td><b>€ K'.$row3['total'].'</b></td>';
echo '  </tr>';
echo '  <tr>';
echo '    <td>%: </td>';
$row0 = 197;
$row1 = 301;

function percent($row0, $row1) {
echo '       <td>'.number_format((1 - $row0 / $row1) * 100, 2);
}

percent($row0, $row1);
'</td>';
echo '       <td>%</td>';
echo '  </tr>';
echo '   </tbody>';
echo '</table>';
echo '</div>';

but $row0 = 197; $row1 = 301;

are hardcoded :(

Your final code would be something like:

echo '    <td>%: </td>';
echo '       <td>'. percent($row0['total'], $row2['total']) .'%</td>';
echo '       <td>'. percent($row1['total'], $row3['total']) .'%</td>';
echo '  </tr>';