2016-03-02 368 views
0

我正在尝试解决这个百分比计算问题,但它今天只是让我困惑。计算百分比错误

下面是代码:

$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.

回答

0

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

这没有奏效。这里是文件:http://myboxv.com/file/1456954898_reporting-customersatisfaction –

+0

但非常感谢你这么想的意思! :D –

+0

你能提供更多信息吗?什么没有工作?你有任何PHP错误?或者数字错了? – Miro