简体   繁体   English

如何在不使用PHP中的sort()函数或SQL中的ORDER BY的情况下使用名称值对表值进行升序和降序排序?

[英]How to sort table values in ascending and descending with name value without using sort() function in PHP or ORDER BY in SQL?

I use the following code to get my resulted table, and I need to sort this table based on Clicking 'ascending' or 'descending' button respectively with my Name Value. 我使用以下代码获取我的结果表,并且需要根据分别使用“名称值”单击“升序”或“降序”按钮对该表进行排序。 How can I make a custom php function for this? 我该如何为此自定义php函数? Any help will be greatly appreciated. 任何帮助将不胜感激。

<?php
require('config.php');
$sql_sel = "select * from contact";
$res_sel = mysql_query($sql_sel);
?>
<table><tr><td>Name </td><td>Email </td><td> Mobile</td><td>Web </td></tr>
<tr>
<?php
    while($row_sel = myqsl_fetch_array($res_sel)){
        $name = $row_sel['name'];
        $email = $row_sel['email'];
        $mobile $row_sel['mobile'];
        $web =$row_sel['web'];
        echo "<td>".$name."</td>";
        echo "<td>".$email."</td>";
        echo "<td>".$mobile."</td>";
        echo "<td>".$web."</td></tr>";

    }
?>
</table>

<input type="button" value="Ascending" onclick="ascending()">
<input type="button" value="Descending" onclick="descending()">

Where I getting table with values from database table as in the order of execution. 我按执行顺序从数据库表中获取具有值的表。 My page having two buttons Ascending and Descending, while clicking on that, it results table values arranging in Ascending and Descending order based on name value. 我的页面具有两个按钮“升序”和“降序”,单击该按钮时,结果表值将基于名称值按升序和降序排列。 What should i code in 'ascending()' and 'descending()' to achieve this?i need this as a custom php function?? 我应该在'ascending()'和'descending()'中编码什么来实现这一目标?我需要将其作为自定义php函数吗?

Let's say you are passing a value called "sort". 假设您要传递一个名为“ sort”的值。 It can be "dsc" for descending, and anything else (or not specified) for ascending. 降序可以是“ dsc”,而升序可以是其他任何(或未指定)。

First, start with parsing the value: 首先,从解析值开始:

$sortMode = 'ASC';
if (array_key_exists('sort', $_REQUEST)) {
    if ($_REQUEST['sort'] == 'dsc') {
        $sortMode = 'DESC'
    }
}

Later on, when you create your query: 稍后,当您创建查询时:

$sql_sel = "select * from contact order by name $sortMode";

This will cause the database to sort in the correct direction. 这将导致数据库按正确的方向排序。

One important thing to note is that you should never ever ever pass user input (eg $_GET, $_POST, etc) directly into your SQL string so that you don't expose yourself to SQL injection attacks . 要注意的一件事是, 永远不要将用户输入(例如$ _GET,$ _ POST等)直接传递到SQL字符串中,以免暴露于SQL注入攻击 Note that here we test the $_REQUEST parameter and set the actual PHP variable manually to avoid this. 请注意,这里我们测试$ _REQUEST参数并手动设置实际的PHP变量以避免这种情况。

In production applications, you should use parameter binding as much as possible (not relevant here since ASC/DESC is part of a statement, not a parameter). 在生产应用程序中,应尽可能使用参数绑定 (此处不相关,因为ASC / DESC是语句的一部分,而不是参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM