我有一个total_amount($ total_amount_column),它是那里Amount列的总和。
在数据库中,我有多个按名称调用的条目,它们都有与之关联的金额。
所以它在名为“envelope_sub”
的表中看起来像这样name |amount |percentage
House |1550.00 |
Electricity |400.00 |
Internet |50.00 |
我需要的是根据总金额列自动计算百分比列。换句话说,我需要每个条目的百分比,并将它们自动插入数据库中各自的百分比字段中。我怎么能做到这一点。
答案 0 :(得分:0)
这是我最终如何做到的
<?php
$query = "SELECT * from stats where user_id='".$nameID."' $where order by week desc";
$result = mysql_query($query) or die(mysql_error());
$worked_week = mysql_num_rows($result);
if(mysql_num_rows($result)) {
$k = 1;
while ($row_stats = mysql_fetch_array($result))
{
// Total Section //
$total_worked_hours=$total_worked_hours+$row_stats['worked_hours'];
$total_sales=$total_sales+$row_stats['sales'];
$total_sales=number_format($total_sales,2,'.','');
$total_facials=$total_facials+$row_stats['facials'];
$total_double_facials=$total_double_facials+$row_stats['double_facials'];
$total_classes=$total_classes+$row_stats['classes'];
$total_faces_viewed=$total_faces_viewed+$row_stats['faces_viewed'];
$total_interviews=$total_interviews+$row_stats['interviews'];
$total_recruits=$total_recruits+$row_stats['recruits'];
///
$k++;
}
// Percentage Section //
$perc_worked_hours = $total_worked_hours / $worked_week;
$perc_sales = $total_sales / $worked_week;
$perc_facials = $total_facials / $worked_week;
$perc_double_facials = $total_double_facials / $worked_week;
$perc_classes = $total_classes / $worked_week;
$perc_faces_viewed = $total_faces_viewed / $worked_week;
$perc_interviews = $total_interviews / $worked_week;
$perc_recruits = $total_recruits / $worked_week;
// round up
$perc_worked_hours=number_format($perc_worked_hours,0,'.','');
$perc_sales=number_format($perc_sales,2,'.','');
$perc_facials=number_format($perc_facials,0,'.','');
$perc_double_facials=number_format($perc_double_facials,0,'.','');
$perc_classes=number_format($perc_classes,0,'.','');
$perc_faces_viewed=number_format($perc_faces_viewed,0,'.','');
$perc_interviews=number_format($perc_interviews,0,'.','');
$perc_recruits=number_format($perc_recruits,0,'.','');
// END