我与PHP和SQL工作的百分比我犯了这样的一个belowe表:获得2号
我得到的所有数据与MySQL一样,你可以在图片中看到。现在我需要计算这两个数字的百分比,例如301和197 =%34.55 这是我在我的脑海中快速做出来的,但是我怎么能在php中为此做出这样的一个方法。
我也没有尝试这个PHP代码来完成这项工程,但是当我复制过去,它并不在我的工作表中,并改变一些代码。
<?php
$row0 = 197;
$row1 = 301;
function percent($row0, $row1) {
echo number_format((1 - $row0/$row1) * 100, 2);
}
percent($row0, $row1);
这是我做的表格:
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>';
----------这些都是从我的数据库来实数!
这样,它似乎很好地工作:
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>';
但 $ 0行= 197; $ row1 = 301;
是硬编码:(
什么是“不工作”的意思的比例是不正确的电脑爆炸,...你有真正的数字,或者你正在计算文本? –
请尝试分开你的HTML和PHP。 – julekgwa
我认为你可以(也可能应该)在MySQL中处理这个问题吗? –