计算 php 代码中每个名称重复名称的分数

count points for duplicate names for each name in php code

我有 table 我从显示 sql 记录中得到它,这个 table 我用 php 代码制作,table 像这样

pos.     name       points

 1       john          8
 2       mike          5
 3       mike          9
 4       john          2

php 此 table 的代码显示来自 phpmyadmin sql

的数据库记录

<?php 

// make connecion
mysql_connect('localhost', 'db user', 'db password');

// Select Database
mysql_select_db ('db name');

$sql="SELECT * FROM wp_wp_pro_quiz_toplist";

$records=mysql_query($sql);



?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>الاحصائيات النهائية لمسابقة اكتوبر</title>
</head>

<body>

<table class="wpProQuiz_toplistTable">
    
   <tr>
    <th style="width: 40px;">Pos.</th>
    <th style="">Name</th>
    <th style="width: 60px;">Points</th>
   <tr>
<?php 

 while($wp_wp_pro_quiz_toplist=mysql_fetch_assoc($records)) {
  
  echo "<tr>"; 
  
  echo "<td>".$wp_wp_pro_quiz_toplist[toplist_id]."</td>"; 
  
  echo "<td>".$wp_wp_pro_quiz_toplist[name]."</td>";
  
  echo "<td>".$wp_wp_pro_quiz_toplist[points]."</td>";
  
  echo "</tr>"; 
  
  
  }// End While

?>               
           
                  
 </table>

</body>
</html>

如您在 table 中所见,我有重复的名称,例如 'mike' 显示了 2 次。

我想对table中的每个名称和数组或顺序名称进行总分,根据收到的每个名称的分数最多,就像过去的table示例

pos.     name       points

 1       mike         14
 2       john         10

请编辑我上面的 php 代码并输入您的答案,因为我是新手,无法使用完美的代码,我需要您的快速帮助,抱歉我的英语很少

您应该更改查询以执行此操作:

mysql> select pos, name, SUM(points) as total from wp_wp_pro_quiz_toplist group by name;
+------+------+--------+
| pos  | name | total  |
+------+------+--------+
|    1 | john |     10 |
|    2 | mike |     14 |
+------+------+--------+
2 rows in set (0.00 sec)

如果你想获得按 点排序的结果,那么你应该发出此查询(考虑到 pos值将是错误的):

mysql> select pos, name, SUM(points) as total from wp_wp_pro_quiz_toplist group by name order by total DESC;
+------+------+-------+
| pos  | name | total |
+------+------+-------+
|    2 | mike |    14 |
|    1 | john |    10 |
+------+------+-------+
2 rows in set (0.00 sec)

试试这个....

$sql="SELECT sum(points) as sumpoints , name FROM wp_wp_pro_quiz_toplist group by name";

$records=mysql_query($sql);