2015-10-16 102 views
0

我使用引導程序進度條顯示統計信息。計算特定行

我想知道總共有多少個得分的10分。

$q="SELECT * FROM users"; 
$r = mysqli_query($con,$q); 
$total = mysqli_num_rows($r); 

Name email   score time 
Hello [email protected] 10 15 
Hello [email protected] 58 10 
Test [email protected] 10 12 
Stack [email protected] 90 20 
Test [email protected] 50 40 
+0

你只想知道有多少得分10?或者你希望它是動態的,並且每個數字都有一個百分比? –

+0

是在行總數中有多少得分爲10,9,8,7,6,5,4,3,2,1 – Roger

回答

0

$ q =「SELECT COUNT(Name)FROM users WHERE score = '10'」;

0

你可以使用這個簡單的查詢,以完成它: -

SELECT COUNT(name), score 
FROM users 
GROUP BY score 

這將獲得總每一個人得分(10,9,8)。 如果您只想要總價值10。改用它。

SELECT COUNT(name), score 
FROM users 
WHERE score=10 
0

通過$總陣列這將循環和分配的所有值小於10到一個數組然後循環通過該陣列打印出多少出總的爲每個小於10

$total = [ 
     0 => [ 
      'name' => 'Hello', 
      'email' => '[email protected]', 
      'score' => 10, 
      'time' => 15 
     ], 
     1 => [ 
      'name' => 'Hello', 
      'email' => '[email protected]', 
      'score' => 58, 
      'time' => 10 
     ], 
     2 => [ 
      'name' => 'Test', 
      'email' => '[email protected]', 
      'score' => 10, 
      'time' => 12 
     ], 
     3 => [ 
      'name' => 'Stack', 
      'email' => '[email protected]', 
      'score' => 90, 
      'time' => 20 
     ], 
     4 => [ 
      'name' => 'Test', 
      'email' => '[email protected]', 
      'score' => 50, 
      'time' => 40 
     ], 
    ]; 

    $sizeOfArray = count($total); 
    $arrayOfScoresLessThanTen = []; 
    foreach($total as $value) { 
     if($value['score'] <= 10) { 
      if(isset($arrayOfScoresLessThanTen[$value['score']])) { 
       $arrayOfScoresLessThanTen[$value['score']] ++; 
      } 
      else { 
       $arrayOfScoresLessThanTen[$value['score']] = 1; 
      } 
     } 
    } 

    foreach($arrayOfScoresLessThanTen as $key => $scoresLessThanTen) { 
     echo 'Score: '. $key . ' number scored out of total: '. $scoresLessThanTen . '/'.$sizeOfArray; 
    } 

這已經過測試,並與我創建的數組$ total一起工作,以匹配您在表中使用的數組。我添加了我的$ total數組,以便您可以自己測試它。