【问题标题】:How to sort rows from an mysql table using order by and order by desc without making it over complicated如何使用 order by 和 order by desc 对 mysql 表中的行进行排序而不使其过于复杂
【发布时间】:2019-08-18 02:24:39
【问题描述】:

我找到了一个简单的解决方案,可以按我的 html 表中的每一列进行排序。 现在我还希望能够按除 desc 之外的每一列进行排序,但是根据我的解决方案的想法,由于 if 中有两个 if,代码看起来过于复杂。

我想不出另一种可能看起来更好且总体上更容易的解决方案。

这是我现在的代码:

<th><a href="mypage.php?sort=type">Type:</a></th>
<th><a href="mypage.php?sort=desc">Description:</a></th>
<th><a href="mypage.php?sort=recorded">Recorded Date:</a></th>
<th><a href="mypage.php?sort=added">Added Date:</a></th>
<?php

$sql = "SELECT * FROM MyTable";

if ($_GET['sort'] == 'type')
{
    $sql .= " ORDER BY type";
}
elseif ($_GET['sort'] == 'desc')
{
    $sql .= " ORDER BY Description";
}
elseif ($_GET['sort'] == 'recorded')
{
    $sql .= " ORDER BY DateRecorded";
}
elseif($_GET['sort'] == 'added')
{
    $sql .= " ORDER BY DateAdded";
}

$>

我的第一个想法是这样的:

$sql = "SELECT * FROM MyTable";

$checkSort = false;
if ($_GET['sort'] == 'type')
{
    if ($checkSort == false)
    {
        $sql .= " ORDER BY type";
        $checkSort = true; 
    }
    if ($checkSort == true)
    {
        $sql .= " ORDER BY type desc";
        $checkSort = false; 
    }
}

我认为它看起来不干净,因为我需要对每一列都这样做,而且我的表将来应该会变成更多列。

【问题讨论】:

  • 所以你正在寻找代码优化?对
  • @devpro 好吧,是的,我还是个初学者,想不出其他更好的解决方案,我想也许有人知道我应该怎么做
  • @Xxy :另一种解决方案是使用database
  • @PraveenKumar 我以前从未使用过数据库,所以我需要先阅读如何包含和使用它。但我也想自己学。
  • 你还想按 DESC 顺序排序吗?

标签: php html mysql sorting


【解决方案1】:

您可以尝试在调用中直接发送值。

<th><a href="mypage.php?sort=type&sortType=ASC/DESC">Type:</a></th>
<th><a href="mypage.php?sort=description&sortType=ASC/DESC">Description:</a></th>
<th><a href="mypage.php?sort=daterecorded&sortType=ASC/DESC">Recorded Date:</a></th>
<th><a href="mypage.php?sort=dateadded&sortType=ASC/DESC">Added Date:</a></th>

<?php
$sql = "SELECT * FROM MyTable ORDER BY ".$_GET['sort']." ".$_GET['sortType'];
?>

显然,当您直接将调用传递给 DB 时,您必须进行完整性检查。

注意 - "&sortType=ASC/DESC",只发送 ASC 或 DESC 之一

【讨论】:

  • DESC 呢??
  • 您要进行降序调用吗?
  • 作为问题中提到的OP,order by和order by desc。我认为这里只是通过 ASC 订购
  • 编辑了两者的响应
  • 编辑会产生语法错误,请在你的机器上运行
【解决方案2】:

我会使用它:(DOC)

$sql = "SELECT * FROM MyTable";

switch($_GET['sort']):
    case 'type':
        $sql .= " ORDER BY type";
        break;
    case 'desc':
        $sql .= " ORDER BY Description";
        break;
    case 'recorded':
        $sql .= " ORDER BY DateRecorded";
        break;
    case 'added':
        $sql .= " ORDER BY DateAdded";
        break;
endswitch;

您可以添加任意数量的“案例”!

【讨论】:

    【解决方案3】:

    您可以使用 datatable 来解决问题。请检查以下代码

    HTML 文件代码

    <!-- Need to add this CSS and JS in your HTML page -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
    <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    
    
    <script type="text/javascript">
        //This code used to initialise the table shown
        $(document).ready(function() {
            $('#example').DataTable( {
                "processing": true,
                "serverSide": true,
                "ajax": "server_processing.php"
            } );
        } );
    </script>
    
    <table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Type:</th>
                <th>Description:</th>
                <th>Recorded Date:</th>
                <th>Added Date:</th>
            </tr>
        </thead>
    </table>
    

    或者你可以查看blog

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多