Laravel 平均值为空值

标签 laravel average

我正在尝试对数据库表中的每一行进行平均。但它无法正常工作,我想忽略该值,如果为空,则它不会计算为零。使用我的代码,它将空值计算为零,我想像 MS Excel 那样执行,如果行/单元格为空,它将忽略。

Controller 代码:

public function update_average(Request $request) 
        { 
          $scores = $request->input('scores'); //here scores is the input array param 
          foreach($scores as $row){ 
          $score = cie_score::find($row['id']); 
         
          $score->term2_average =round( ($row['term1_result'?: null] + $row['term2_result'?: null]) /2);
          $score->term3_average =round( ($row['term1_result'?: null] + $row['term2_result'?: null] + $row['term3_result'?: null])/3);
          $score->term4_average =round( ($row['term1_result'?: null] + $row['term2_result'?: null] + $row['term3_result'?: null] + $row['term4_result'?: null])/4);
       
          $score->save(); 
          }
          
        

          return redirect()->back();
        }

图像结果 enter image description here

最佳答案

您可以按如下方式计算您要查找的内容:

    $currentTerm = 4;
    $scores = $request->input('scores');

    for ($term = 1; $term <= $currentTerm; $term++) {
        foreach ($scores as $row) {
            $score = cie_score::find($row['id']);

            $total = 0;
            $terms = 0;

            for ($i = 1; $i <= $term; $i++) {
                $total += $row['term'.$i.'_result'] ?? 0;
                $terms += empty($row['term'.$i.'_result']) ? 0 : 1;
            }

            $key = 'term'.$term.'_average';
            
            if ($terms > 0) {
                $score->$key = round($total/$terms);
            } else {
               $score->$key = 0;
            }
           
            $score->save();
        }
    }

这将计算从 1 到当前术语的所有术语的平均值。如果“term{$i}_result”为空,则它在总数中被忽略,并且不会计入除数。

您的数据库执行以下操作会更有效:

    $currentTerm = 4;
    $averages = [];
    $scores = $request->input('scores');

    for ($term = 1; $term <= $currentTerm; $term++) {
        foreach ($scores as $row) {
            $id = $row['id'];
            if (!isset($averages[$id])) {
                $averages[$id] = [];
            }

            $total = 0;
            $terms = 0;

            for ($i = 1; $i <= $term; $i++) {
                $total += $row['term'.$i.'_result'] ?? 0;
                $terms += empty($row['term'.$i.'_result']) ? 0 : 1;
            }

            $key = 'term'.$term.'_average';
            
            if ($terms > 0) {
                $averages[$id][$key] = round($total/$terms);
            } else {
                $averages[$id][$key];
            }
        }

        foreach($averages as $id => $data) {
            cie_score::where('id', $id)
                     ->update($data);
        }
    }

关于Laravel 平均值为空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62949635/

相关文章:

mysql - laravel REST api post 方法

laravel - 如何在 Laravel 迁移和模型中建立自反关系

php - 如何检查用户是否订阅了 View 中的计划?

python - 查找列表的平均值

algorithm - 计算实际平均值

php - Laravel Horizo​​n 中只执行一次作业的设置是什么?

mysql - 如何删除元组作为工资低于平均水平?

python - Python NumPy 中的 np.mean() 与 np.average()?

excel - 3 Excel 中的变量加权平均值

laravel - CORS Laravel VueJS