-1

そこにある Amount 列の合計である total_amount ($total_amount_column) があります。
データベースには、名前で呼び出される複数のエントリがあり、それらにはすべて金額が関連付けられています。

したがって、「envelope_sub」というテーブルでは次のようになります

name         |amount          |percentage

House        |1550.00         |

Electricity  |400.00          |

Internet     |50.00           |

必要なのは、合計金額の列に基づいてパーセンテージ列を自動的に計算することです。つまり、各エントリのパーセンテージ比率が必要で、データベースのそれぞれのパーセンテージ フィールドに自動的に挿入します。どうすればそれを達成できますか。

4

1 に答える 1

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
于 2013-09-06T21:07:22.507 に答える