Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中从mysql数据库中的总和计算百分比比率_Php_Percentage - Fatal编程技术网

在php中从mysql数据库中的总和计算百分比比率

在php中从mysql数据库中的总和计算百分比比率,php,percentage,Php,Percentage,我有一个总金额($total\u amount\u column),它是下面金额列的总和。 在数据库中,我有多个名为的条目,它们都有一个关联的金额 所以它看起来像是表中的“信封_sub” 我需要的是根据总额列自动计算百分比列。换句话说,我需要每个条目的百分比,并将它们自动插入数据库中各自的百分比字段中。请告诉我如何才能做到这一点。以下是我最终如何做到的 <?php $query = "SELECT * from stats where user_id='".$na

我有一个总金额($total\u amount\u column),它是下面金额列的总和。
在数据库中,我有多个名为的条目,它们都有一个关联的金额

所以它看起来像是表中的“信封_sub”


我需要的是根据总额列自动计算百分比列。换句话说,我需要每个条目的百分比,并将它们自动插入数据库中各自的百分比字段中。请告诉我如何才能做到这一点。

以下是我最终如何做到的

        <?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

这里有一个类似的(可能重复的)问题:这里有另一个:嗨,我不认为这对我正在寻找的东西有什么帮助。我需要知道基于金额列的总和,每个金额所代表的百分比。
        <?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