php - 通过单击列名称对行进行排序

标签 php html mysql mysqli

我必须通过单击列名来对显示的列表进行排序。我的灵感来自 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/

相关文章:

mysql - 我该如何编写此查询

mysql - 按字母顺序对 MySQL 结果排序,但数字排在最后

javascript - 将值附加到 Javascript 表单上的 URL

html - 搜索焦点和出局

javascript - jquery ui draggable 自动改变宽度

javascript - 如何通过单击第 1 页中的按钮更改第 2 页中 html 的颜色

php - SAS Azure 签名不匹配

php/curl - 使用 header 获取远程服务器上的图像扩展

PHP文件://wrapper

php - 如何使用单选按钮更新 MySQL 中的数据?