doubi3996 2016-07-29 08:41
浏览 8

如何计算某个数据点上方的表中的行?

I have made a leaderboard for a quiz ordered by their score and I want to show the position of the user. How could I count how many rows are above them in the table in order to determine their position? This code only works when people don't have the same score. If 2 people both have the high score, both will see themselves as number 2.

function firstPlace() {
    include 'connection.php';
    $sql="SELECT * FROM Users WHERE Email='$_SESSION[email]'";
    $result=mysqli_query($connection,$sql);
    while($row1=mysqli_fetch_array($result)) {
        if(isset($row1[4])) {
            $score=$row1[4];
        }
    }
    $sql2="SELECT * FROM Users WHERE Email='$_SESSION[email]'";
    $result2=mysqli_query($connection,$sql2);
    $sql1="SELECT * FROM Users WHERE Score >= $score ORDER BY Score DESC";
    $result1=mysqli_query($connection,$sql1);
    $numRows=mysqli_num_rows($result1);

    while($row=mysqli_fetch_array($result2)) {
        if($row[5]!="") {
            $src1="photos/".$row[5];
        }
        else {
            $src1='images/profile-pic.png';
        }
        echo "
        <a  class='logo' href='index.php'><img src='$src1' class='bigProfilePic' id='logo' alt='Profile pic'></a>
        <h1>$numRows</h1>
        <h2>$row[1]</h2>
        <h3>$row[4]<span>Kms</span></h3>";
    }
  • 写回答

1条回答 默认 最新

  • doutou6803 2016-07-29 09:37
    关注
    $sql = 'SELECT count(id) FROM Users WHERE Score > ?';
    $stmt = mysqli_prepare($sql);
    $stmt->bind_param("i", $Score );
    $stmt->execute();
    $result = $stmt->get_result();
    

    Will give you how many users have score higher than $score, you could of course also keep the query open for sql injction as the others if you want to

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c