dongzhao1930 2017-04-11 08:16
浏览 41

获得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 :(

  • 写回答

1条回答 默认 最新

  • douwei7501 2017-04-11 08:19
    关注

    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>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分