我必须通过单击列名来对显示的列表进行排序。我的灵感来自 this thread。 , 但不知道如何继续。
这是我的代码:
<?php
$result = mysqli_query($mysqli,"SELECT rid, pid, firstname, lastname, email FROM temp_members_db WHERE pid='$pid1' ");
echo "<br><table><tr class='tabletop'><th><a href='mypage.php?sort=type'>Namn</a></th><th><a href='mypage.php?sort=type'>E-mail</a></th><th><a href='mypage.php?sort=type'>Resultat</a></th><th>Ta bort kandidat</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td><strong>
<form action='respondent2.php' method='GET'>
<input type='hidden' name='rid' value='".$row['rid']."'>
<input type='hidden' name='firstname' value='".$row['firstname']."'>
<input type='submit' class='resname' name='submit' value='".$row['firstname']." ".$row['lastname']."'>
</form>
</strong></td>
<td>".$row['email']."</td>
<td><strong><span style=\"color: $color\">".$result00."</span>%</strong></td>
<form action='deleterespondent2.php' method='post'>
<input type='hidden' name='rid' value='".$row['rid']."'>
<td> <input type='submit' class='mydel' value='Radera' onclick=\"return confirm('Show me!')\">
</form>
</td></tr>";
}
echo "</table>";?>
如您所见,我已经开始编写 <a href="mypage.php?sort=type">
在每个列名上,不知道要写什么而不是“类型”。我想要的是能够单击列标题并根据所选列对下面的行进行排序,例如名称。然后,当然,我希望所属的行彼此跟随,而不仅仅是更改顺序的所选列。
最佳答案
将您的代码更改为:
if ($_GET['sort'] == 'namn')
{
$order = " ORDER BY lastname";
}
elseif ($_GET['sort'] == 'email')
{
$order = " ORDER BY email";
}
$result = mysqli_query($mysqli,"SELECT rid, pid, firstname, lastname, email FROM temp_members_db WHERE pid='$pid1' ".$order);
echo "<br><table><tr class='tabletop'><th><a href='mypage.php?sort=namn'>Namn</a></th><th><a href='mypage.php?sort=email'>E-mail</a></th><th>Resultat</th><th>Ta bort kandidat</th></tr>";
关于php - 通过单击列名称对行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40058872/