【问题标题】:Calculating Percentages Error计算百分比错误
【发布时间】:2016-06-15 22:01:31
【问题描述】:

我正在尝试修正这个百分比计算,但是今天它只是难住了我。

代码如下:

$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);

我有表格,但表格上有不适用的单选按钮。当客户填写表格时,有时在某些问题上他们需要不适用,因为它们不适用并且不需要计入总分。

这就是生成的 % 不正确的报告。该百分比应为:94%。在这张图片中,您将看到,如果您点击图表,您可以看到:

Graph Once Clicked

它显示了回答这个问题的人,有 20 个。每个人总共有 5 个最高分,或者在这种情况下,我将 N/A 框设置为空白,返回 0。什么它正在做的是将所有可能的分数加起来为 100。(20 人和 5 最大分数)

我需要它做的是 计算空白字段,作为回报,例如在图片中给我Graph Once Clicked 只有 5 人回答,所以最高分是 25。总分点数是 23.5 所以 23.5 / 25。

【问题讨论】:

    标签: php jquery jquery-calculation


    【解决方案1】:

    这段代码怎么样?已回答问题的总百分比在变量 $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;
    

    【讨论】:

    • 但是非常感谢您尝试这意味着这么多! :D
    • 您能提供更多信息吗?到底什么没用?你有任何PHP错误吗?还是号码错了?
    • 保持在 23.5% 的数字没有变化。
    • 如果有帮助,我还添加了 php 文件本身。 :)
    猜你喜欢
    • 2016-09-04
    • 1970-01-01
    • 2011-06-01
    • 2021-01-10
    • 2021-05-05
    • 2018-04-14
    • 1970-01-01
    相关资源
    最近更新 更多