duanfan8360 2016-03-02 20:37
浏览 151

计算百分比误差

I am trying to fix this percent calculation, however it is just stumping me today.

Here is the code:

$entries = GFAPI::get_entries($form['id'], $search_criteria);

$score = 0;
$max = 0;
$percentage = array();
if(!empty($entries)) {
    foreach ($entries as $entry) {

        $score = GFSurvey::get_field_score($form_fields, $entry);
        $max = end($form_fields['choices']);

        if(empty($max['score'])) {
            unset($form_fields['choices'][key($form_fields['choices'])]);
            $max = end($form_fields['choices']);
            }
        $max = $max['score'];
        $percentage[] = ($score / $max ) * 100;
        }
    }

$average = round(array_sum($percentage) / count($percentage), 2);

I have the form and i have Not Applicable radio buttons on the form. When a client fills out the form, sometimes on certain questions they need to be N/A because they do not apply and that does not need to counted in the overall total score.

So that is the report generated which the % is incorrect. That percent should read: 94%. In this picture you will see if you click on the graph you can see this:

Graph Once Clicked

It is showing the people who answered this question, and there are 20. There are a total of 5 max points for each person, or in this case I have the N/A box set for blank, which returns 0. What it is doing is totaling all the possible points which are 100. (20 people and 5 max points)

What I need it to do is NOT count the blank fields and in return give me for example in the image Graph Once Clicked there are only 5 people that answered so max points are 25. the total points is 23.5 so 23.5 / 25.

  • 写回答

1条回答 默认 最新

  • dongwuwu6104 2016-03-02 20:57
    关注

    How about this code ? The total percentage of answered questions is in variable $total_percentage.

    $entries = GFAPI::get_entries($form['id'], $search_criteria);
    
    $score = 0;
    $max = 0;
    $total_max = $total_score = 0;
    $percentage = array();
    if(!empty($entries)) {
        foreach ($entries as $entry) {
    
            $score = GFSurvey::get_field_score($form_fields, $entry);
            $max = end($form_fields['choices']);
    
            if(empty($max['score'])) {
                unset($form_fields['choices'][key($form_fields['choices'])]);
                $max = end($form_fields['choices']);
                }
            $max = $max['score'];
    
            if ($max) {
                $total_score += $score;
                $total_max += $max;
            }
    
            $percentage[] = ($score / $max ) * 100;
            }
        }
    
    $average = round(array_sum($percentage) / count($percentage), 2);
    $total_percentage = ($total_max > 0) ? round($total_score/$total_max/100, 2) : 0;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?