Cla*_*ado 3 php sorting while-loop
我为我的学生做了一个简单的基于PHP的考试.结果在数据库中记录为"q1","q2","q3","q4"和"q5".
正确答案记录为"是",错误答案记录为"否".
有5个问题,我希望总分为100分.
所有的功能都按照我的意愿运行,但问题是,我不知道如何将结果从高到低排序.任何帮助都非常感谢!
$result = mysqli_query($db,"SELECT * FROM table");
while($row = mysqli_fetch_array($result)){
$score = 0;
echo '<img alt="'.$row["name"].'" src="images/'.$row["name"].'.jpg" width="118" height="158"';
if ($row["q1"] === "yes") {$score=$score+20;}
if ($row["q2"] === "yes") {$score=$score+20;}
if ($row["q3"] === "yes") {$score=$score+20;}
if ($row["q4"] === "yes") {$score=$score+20;}
if ($row["q5"] === "yes") {$score=$score+20;}
echo ' /> '.$row["name"] . ' ' . $score;
}
Run Code Online (Sandbox Code Playgroud)
取而代之的是while,内if,你可以做一切与select和order by结果列如:
select name, if(q1='yes', 20, 0) +
if(q2='yes', 20, 0) +
if(q3='yes', 20, 0) +
if(q4='yes', 20, 0) +
if(q5='yes', 20, 0) result
from table
order by result desc
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |