gpt4 book ai didi

php - 从未存储在 mysql 中的 PHP 中获取 TOTAL 值的排名

转载 作者:行者123 更新时间:2023-11-28 23:28:51 24 4
gpt4 key购买 nike

美好的一天,我正在开发一些评分系统,它可以显示所有用户的所有分数,并使用 MySQL 中的 PHP 对其进行汇总。结果工作正常,计算也很好(计算是用 php 完成的,而不是在 MySQL 上完成的)。现在我的问题是,如何将总分从高到低排序。

代码如下:

$sel_query="Select * from tbl_scores";
$result = mysql_query($sel_query);
while($row = mysql_fetch_array($result)) {
$crit_3 = $row["crit_3"];
$crit_3a = number_format($crit_3);

$crit2_3 = $row["crit2_3"];
$crit2_3a = number_format($crit2_3);

$crit3_3 = $row["crit3_3"];
$crit3_3a = number_format($crit3_3);

$user1 = ($crit_3) ;
$user2 = ($crit2_3);
$user3 = ($crit3_3);
$divide = ($user1 + $user2 + $user3);
$total = number_format($divide / 9 , 2, '.', '');
$average = number_format($total * 0.15 , 2, '.', '');
?>

提前致谢。

最佳答案

1.请停止使用(从 php5.5 弃用 + 从 php7 移除)mysql_*,使用 mysqli_*PDO

2.在循环外定义一个数组变量,在循环内将总分赋给该数组。

3.现在你会得到一个分数数组,现在你可以使用rshort()方法来正确获取数据。

所以代码应该像下面这样:-

$sel_query="Select * from tbl_scores";
$result = mysql_query($sel_query);
$scores_array = array();//create an array
while($row = mysql_fetch_array($result)) {
$crit_3 = $row["crit_3"];
$crit_3a = number_format($crit_3);

$crit2_3 = $row["crit2_3"];
$crit2_3a = number_format($crit2_3);

$crit3_3 = $row["crit3_3"];
$crit3_3a = number_format($crit3_3);

$user1 = ($crit_3) ;
$user2 = ($crit2_3);
$user3 = ($crit3_3);
$divide = ($user1 + $user2 + $user3);
$total = number_format($divide / 9 , 2, '.', '');
$average = number_format($total * 0.15 , 2, '.', '');
$scores_array[$row['user_name']] = $total; // assign total to the array and i assume that your table has one column name user_name for each user, change accordingly
}
rsort($scores_array);// sort the array in decending order of total scores
foreach ($scores_array as $key=>$value){ // iterate through array
echo $key.'has scored total score:-'.$value; //print the score along with username
}
?>

注意:- 请以不会产生任何歧义的方式使用变量名。

关于php - 从未存储在 mysql 中的 PHP 中获取 TOTAL 值的排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38114102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com