2015-11-05 146 views
0

我有一些代码将WP中GravityForms测验的变量传递给结果表,并且可视化地处理与其余答案不同的高分。计算第二和第三高分

我在尝试计算第二个和第三个高分时遇到了问题,并且还会将这些变量传递到表中以与其余答案显示为不同。

这里是高分代码:

<?php 

     $results = $_REQUEST['results']; 

     $high_score = 0; 

     // Find the highest score 
     foreach ($results as $result) { 
      if ($result >= $high_score) { 
      $high_score = $result; 
      } 
     } 
    ?> 

这里是代码显示的答案:

<?php 
      for ($i = 0; $i < 9; $i++) { 
       $type = $i + 1; 

       if ($results[$i] == $high_score) { 
        echo '<td data-th="Type ' . $type . '" style="text-align: center; color: #42459c;"><strong>' . $results[$i] . '</strong></td>'; 
       } else { 
        echo '<td data-th="Type ' . $type . '" style="text-align: center; color: #3F4346;">' . $results[$i] . '</td>'; 
       } 
      } 
     ?> 

我怎样才能找到第二个和第三个得分最高?

任何帮助将是巨大的!谢谢!

回答

0

这会帮你吗?排序结果数组从最高到最低的是这样的:

$results = $_REQUEST['results']; 
rsort($results); 
$results[0]; // Highest score 
$results[1]; // Second highest score 
$results[2]; // Third highest score 

显示的结果(如果只想9):

for ($i = 0; $i < 9; $i++) { 
    if($i < count($results)){ 
     $type = $i + 1; 
     if ($i == 0) { 
      echo '<td data-th="Type' . $type . '" style="text-align: center; color: #42459c;"><strong>' . $results[$i] . '</strong></td>'; 
     } else { 
      echo '<td data-th="Type' . $type . '" style="text-align: center; color: #3F4346;">' . $results[$i] . '</td>'; 
     } 
    } 
} 

如果你想明确地说,第一名是一种颜色,第2放置另一种颜色等等,这可以工作:

foreach($results as $i => $result) { 
    $type = $i+1; 
    if($i == 0) // First 
     echo '<td data-th="Type' . $type . '" style="text-align: center; color: red;"><strong>' . $result . '</strong></td>'; 
    else if($i == 1) // Second 
     echo '<td data-th="Type' . $type . '" style="text-align: center; color: blue;"><strong>' . $result . '</strong></td>'; 
    else if($i == 2) // Third 
     echo '<td data-th="Type' . $type . '" style="text-align: center; color: green;"><strong>' . $result . '</strong></td>'; 

    // All other results 
    else 
     echo '<td data-th="Type' . $type . '" style="text-align: center; color: gray;">' . $result . '</td>'; 
} 
+0

请务必在使用第二个(或第三个)条目之前检查是否至少有2(3)个结果。 – wallyk

+0

不,不幸的是(至少我不这么认为)...结果类别是静态的(例如,类型1,类型2,类型3,类型4等),但每个类别的答案都是基于动态的用户的输入......所以五个不同的人可以得到5种不同的结果,类型1取决于他们的回答......我需要在视觉上区分前3个答案,而不管他们出现在哪里...和情节扭曲我忘了提及......对于不同的类型也可能有相同的结果......所以说有6个类别,并且类别的结果是1,3,3,4,6,1。我需要大胆的3,3,4,6 ... – kdruss

+0

@kdruss我必须承认,我没有完全遵循你的需要。 – bmla

相关问题