Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据顺序获取每行的位置_Php_Mysql_Sql_Mysqli - Fatal编程技术网

Php 根据顺序获取每行的位置

Php 根据顺序获取每行的位置,php,mysql,sql,mysqli,Php,Mysql,Sql,Mysqli,我有一个highscore表,它有三列:name、highscore、rep。目前我循环查看rep排序的结果。我在循环中有一个$I est,它显示为rank,例如: SELECT highScore, name, rep FROM table1 ORDER BY rep DESC while ($row = $result->fetch_assoc()) { echo "<tr>"; echo

我有一个highscore表,它有三列:name、highscore、rep。目前我循环查看rep排序的结果。我在循环中有一个$I est,它显示为rank,例如:

SELECT highScore, name, rep FROM  table1 ORDER BY rep DESC

while ($row = $result->fetch_assoc()) 
        {
                echo "<tr>";
                echo "<td>" . $i . "</td>"; // rank
                echo "<td>" . $row["name"] . "</td>"; // name
                echo "<td>" . $row["rep"] . "</td>"; // rep
                echo "<td>" . $row["highScore"] . "</td>"; // high score
                echo "</tr>";
                $i++;
            }
        }
目前这项工作没有故障。问题是我现在实现了一个sortby按钮,它将SQL改为ORDER为highScore而不是rep。唯一的问题是排名现在不匹配。是否有一种方法可以根据rep获取并回显每一行的位置,然后根据highScore进行排序和排序

rank包含您的rank by rep,该表现在按highscore排序。

您能试试这个吗

       SELECT highScore, name, rep,pos FROM 
        (
        SELECT t.name, @rownum := @rownum + 1 AS pos
        FROM `table1` t, (SELECT @rownum := 0) r
        ORDER BY t.rep DESC) `table1` 

    while ($row = $result->fetch_assoc()) 
    {
            echo "<tr>";
            echo "<td>" . $i . "</td>"; // rank
            echo "<td>" . $row["name"] . "</td>"; // name
            echo "<td>" . $row["rep"] . "</td>"; // rep
            echo "<td>" . $row["highScore"] . "</td>"; // high score
            echo "<td>" . $row["pos"] . "</td>"; // position
            echo "</tr>";
            $i++;
        }

谢谢,我正在尝试,但似乎出现了一个错误。我被别名搞糊涂了?你需要给每个子查询一个别名。
       SELECT highScore, name, rep,pos FROM 
        (
        SELECT t.name, @rownum := @rownum + 1 AS pos
        FROM `table1` t, (SELECT @rownum := 0) r
        ORDER BY t.rep DESC) `table1` 

    while ($row = $result->fetch_assoc()) 
    {
            echo "<tr>";
            echo "<td>" . $i . "</td>"; // rank
            echo "<td>" . $row["name"] . "</td>"; // name
            echo "<td>" . $row["rep"] . "</td>"; // rep
            echo "<td>" . $row["highScore"] . "</td>"; // high score
            echo "<td>" . $row["pos"] . "</td>"; // position
            echo "</tr>";
            $i++;
        }