-1

次のデータが格納されている一連の配列があります。

このループはデータを保存します。

   while($stmntResult = mysql_fetch_assoc($stmnt))

    {          
         $userResult[] = $stmntResult["user"];
         $nameResult[] = $stmntResult["full_name"];
         $salescountResult[] = $stmntResult["sumcountSamnt"];
         $upResult[] = $stmntResult["sumcountupAmnt"];
         $countverifiedccResult[] = $stmntResult["sumccverifiedcountAmnt"];
         $amountverifiedccResult[] = $stmntResult["sumccverifiedAmnt"];
         $countverifiedbankResult[] = $stmntResult["sumbankverifiedcountAmnt"];
         $amountverifiedbankResult[] = $stmntResult["sumbankverifiedAmnt"];
         $damounttotalResult[] = $stmntResult["sumDamnt"];
         $upamounttotalResult[] = $stmntResult["sumUpamnt"];
         $mdamounttotalResult[] = $stmntResult["sumMdamnt"];
         $samounttotalResult[] = $stmntResult["sumSamnt"];
         $tmpUser[] = $stmntResult["user"];
         //echo $testResult[$z]. "-" . $nameResult[$z]."<br>";


    }
  $userid Array ( [0] => gdx [1] => hjx [2] => rnx [3] => tbr [4] => tdr [5] => tdy [6] => ter [7] => teu [8] => thr [9] => tht [10] => tje [11] => tjt [12] => toy [13] => tst [14] => ttu [15] => tvy [16] => twu [17] => twy [18] => txy [19] => tyu [20] => tze [21] => tzw [22] => gdx [23] => hjx [24] => rnx [25] => tbr [26] => tcx [27] => tdr [28] => tdy [29] => ter [30] => teu [31] => thr [32] => tht [33] => tje [34] => tjt [35] => toy [36] => tst [37] => ttu [38] => tvy [39] => twu [40] => twy [41] => txy [42] => tyu [43] => tze [44] => tzw [45] => gdx [46] => rnx [47] => tbr [48] => tdr [49] => tdy [50] => teu [51] => thr [52] => tje [53] => tjt [54] => toy [55] => tst [56] => ttu [57] => tvy [58] => twy [59] => tyu [60] => tze [61] => tzw )

 $sales Array ( [0] => 4 [1] => 6 [2] => 6 [3] => 4 [4] => 5 [5] => 1 [6] => 7 [7] => 2 [8] => 1 [9] => 4 [10] => 5 [11] => 9 [12] => 4 [13] => 2 [14] => 3 [15] => 2 [16] => 7 [17] => 2 [18] => 2 [19] => 6 [20] => 3 [21] => 6 [22] => 7 [23] => 5 [24] => 3 [25] => 1 [26] => 0 [27] => 6 [28] => 5 [29] => 4 [30] => 5 [31] => 3 [32] => 6 [33] => 7 [34] => 7 [35] => 5 [36] => 5 [37] => 7 [38] => 6 [39] => 3 [40] => 5 [41] => 3 [42] => 3 [43] => 5 [44] => 3 [45] => 6 [46] => 0 [47] => 5 [48] => 11 [49] => 0 [50] => 6 [51] => 0 [52] => 0 [53] => 8 [54] => 0 [55] => 7 [56] => 3 [57] => 0 [58] => 1 [59] => 2 [60] => 0 [61] => 7 ) 

各配列は多くの場合3回複数回ループされるため、たとえばuserid配列にはgdxの3つのインスタンスがあり、各ユーザーの合計を表示したいと考えています。現在、各ループを一時データベースに挿入しています

    for($i = 0; $i  < count($nameResult); ++$i)
  {

 $testinsert = "insert into stats2 (userid,
                fullname,
                salescountTotal,
                upamountcount,
                countvrfycc,
                amountvrfycc,
                countvrfybank,
                amountvrfybank,
                donationTotal,
                upsaleTotal,
                monthlysaleTotal,
                salesTotal,
                callCount,
                lastupdate)
          VALUES
               ('$userResult[$i]','$nameResult[$i]',
                 '$salescountResult[$i]',
                 '$upResult[$i]',
                 '$countverifiedccResult[$i]',
                 '$amountverifiedccResult[$i]',
                 '$countverifiedbankResult[$i]',
                 '$amountverifiedbankResult[$i]',
                 '$damounttotalResult[$i]',
                 '$upamounttotalResult[$i]',
                 '$mdamounttotalResult[$i]',
                 '$samounttotalResult[$i]',
                 '$callCount[$i]',


                 '$update')
          ";
    mysql_query($testinsert);

次に、再クエリで合計して、以下の出力を取得します

   Name | Sales
   gdx  |  9
   hjx  |  6

サーバーの CPU 時間を節約するために PHP 側で計算を行いたいと思っていました。ご協力ありがとうございます。

各行は、array_sum を使用して各ユーザー データの合計を出力する必要があります。すべてのユーザーの合計を取得できますが、最初に一時データベースに挿入することなく各ユーザーの合計を取得したいと考えています。

4

1 に答える 1