通过单击列名来排序行

时间:2016-10-15 12:12:43

标签: 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">,而不知道要写什么而不是&#34;键入&#34;。我想要的是能够单击列标题并根据所选列对下面的行进行排序,例如Name。然后,当然,我希望所属的行彼此跟随,而不仅仅是所选的列更改顺序。

2 个答案:

答案 0 :(得分:0)

将您的代码更改为:

private Renderer[] sueloRenderers;

void Start()
{
    GameObject[] suelo = GameObject.FindGameObjectsWithTag("SueloWireframe");
    sueloRenderers = new Renderer[suelo.Length];
    for (int i = 0; i < sueloRenderers.Length; i++)
    {
        sueloRenderers[i] = suelo[i].GetComponent<Renderer>();
    }
}

void OnTriggerEnter(Collider other)
{

    if (other.gameObject.tag == "Player")
    {
        EnableRenderer(sueloRenderers, false);
        Debug.Log("Oculta suelo");

    }
}

void OnTriggerExit(Collider other)
{

    if (other.gameObject.tag == "Player")
    {
        EnableRenderer(sueloRenderers, true);
        Debug.Log("Aparece suelo");

    }
}

void EnableRenderer(Renderer[] rd, bool enable)
{
    for (int i = 0; i < rd.Length; i++)
    {
        rd[i].enabled = enable;
    }
}

答案 1 :(得分:0)

请使用参数化查询

$mysqli = new mysqli('host', 'user', 'pass', 'db', port);
if($result = $mysqli -> prepare("SELECT rid, pid, firstname, lastname, email FROM temp_members_db WHERE pid=?")) {
    $result -> bind_param("i", $pid1); // Assuming pid1 is an integer
    $result -> execute();
    $result -> store_result();
    $result -> bind_result($pid);
    while($result -> fetch()) {
      echo $pid; // etc etc
    }  
}

为你读书:

  1. http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
  2. https://www.owasp.org/index.php/Query_Parameterization_Cheat_Sheet
  3. 类似于Drew的建议:

    <?php
    $mysqli = new mysqli('host', 'user', 'pass', 'db', port);
    $order_string = ""
    // Assuming PHP >= 5.4, otherwise use array()
    $sort_keywords = [
        "namn" => "lastname",
        "email" => "email",
    ];
    if(isset($_GET['sort']) && array_key_exists($_GET['sort'], $sort_keywords)) {
        $order_string = " ORDER BY ".$sort_keywords[$_GET['sort']];
    }
    // Assume <head>, <body> etc etc here.
    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>";
    
    if($result = $mysqli -> prepare("SELECT rid, pid, firstname, lastname, email FROM temp_members_db WHERE pid=?".$order_string)) {
        $result -> bind_param("i", $pid1); // Assuming pid1 is an integer
        $result -> execute();
        $result -> store_result();
        $result -> bind_result($rid, $pid, $firstname, $lastname, $email);
        while($result -> fetch()) {
            // $color? $result00?
            echo "<tr><td><strong>
            <form action='respondent2.php' method='GET'>
             <input type='hidden' name='rid' value='".$rid."'>
             <input type='hidden' name='firstname' value='".$firstname."'> 
            <input type='submit' class='resname' name='submit' value='".$firstname." ".$lastname."'>
             </form>
             </strong></td> 
             <td>".$email."</td> 
             <td><strong><span style=\"color: $color\">".$result00."</span>%</strong></td>   
             <form action='deleterespondent2.php' method='post'>
             <input type='hidden' name='rid' value='".$rid."'> 
            <td> <input type='submit' class='mydel' value='Radera' onclick=\"return confirm('Show me!')\">
             </form>
            </td></tr>";
        }  
    }
    
    echo "</table>";
    ?>
    

    或(完全相同):

    <?php
    $mysqli = new mysqli('host', 'user', 'pass', 'db', port);
    $order_string = ""
    // Assuming PHP >= 5.4, otherwise use array()
    $sort_keywords = [
        "namn" => "lastname",
        "email" => "email",
    ];
    if(isset($_GET['sort']) && array_key_exists($_GET['sort'], $sort_keywords)) {
        $order_string = " ORDER BY ".$sort_keywords[$_GET['sort']];
    }
    // Assume <head>, <body> etc etc here.
    ?>
    <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
    if($result = $mysqli -> prepare("SELECT rid, pid, firstname, lastname, email FROM temp_members_db WHERE pid=?".$order_string)) {
        $result -> bind_param("i", $pid1); // Assuming pid1 is an integer
        $result -> execute();
        $result -> store_result();
        $result -> bind_result($rid, $pid, $firstname, $lastname, $email);
        while($result -> fetch()) {
            // $color? $result00?
            ?>
            <tr><td><strong>
            <form action='respondent2.php' method='GET'>
             <input type='hidden' name='rid' value='<?php echo $rid; ?>'>
             <input type='hidden' name='firstname' value='<?php echo $firstname; ?>'> 
            <input type='submit' class='resname' name='submit' value='<?php echo $firstname; ?> <?php echo $lastname; ?>'>
             </form>
             </strong></td> 
             <td><?php echo $email; ?></td> 
             <td><strong><span style="color: <?php echo $color; ?>"><?php echo $result00; ?></span>%</strong></td>   
             <form action='deleterespondent2.php' method='post'>
             <input type='hidden' name='rid' value='<?php echo $rid; ?>'> 
            <td> <input type='submit' class='mydel' value='Radera' onclick="return confirm('Show me!')">
             </form>
            </td></tr>
        <?php
        }  
    }
    ?>
    </table>
    

    此外,最好在数据库中定义排序数组,并将它们echo作为html表行标题中的属性。我个人只是在客户端排序,实际上有时更快(ORDER BY可能很慢),但是,我不知道你的意图。

    享受编辑乐趣;)

相关问题