【问题标题】:HTML TABLE PHP MySQL toggle MySQL sorting order ASC DESC on column header clickHTML TABLE PHP MySQL 切换 MySQL 排序顺序 ASC DESC 点击列标题
【发布时间】:2019-03-21 06:56:44
【问题描述】:

编辑:从接受的答案中添加解决方案

我在这里问的提示是最聪明的方法。

首先需要说明的是:我恳请您避免建议数据库和类似内容。

我在 HTML 表格中显示 MySQL 表格内容。

我的视图是 showdbtable.php,它用作包含 echohtmltable.php,它查询数据库并回显表行。

所以在echohtmltable.php,目前这个问题涉及的东西相当多

获取变量检查

if(isset($_GET['sortby'])){
    $sortby=$_GET['sortby'];
    $_GET['sortby']=''; }
else { 
    $sortby="id_ord"; }

以及随之而来的查询

$query = "
        SELECT id_ord, fornitore, negozio, data_insord, data_prevcons 
        FROM tesord 
        ORDER BY ".$sortby." DESC 
        LIMIT :from_record_num, :records_per_page";

下一个用于回显表格行,同时在列标题中创建排序机制

echo "<th>".'<a href="showdbtable.php?sortby=fornitore">' . "Fornitore</th>";
echo "<th>".'<a href="showdbtable.php?sortby=negozio">' . "Negozio</th>";
echo "<th>".'<a href="showdbtable.php?sortby=data_insord">' . "Data_insord</th>";
echo "<th>".'<a href="showdbtable.php?sortby=data_prevcons">' . "Data_prevcons</th>";

如您所知,这第一部分有效。

当我单击上述链接之一时,查询有效,但目前从代码中可以明显看出,仅在 DESC 中。

善意的请求完全是一个建议。但请注意,我有分页,你看,LIMIT :from_record_num, :records_per_page,应该考虑到这一点。

这是在 ASC 和 DESC 之间切换的最智能和最有效的方式,即在单击一个链接后,例如“Negozio”,再次点击“Negozio”,它会在ASC中排序,然后点击它会切换DESC,然后点击ASC等等。

我确信查询会发生变化,因为我可以调用 $ascdesc 的变量

$query = "
        SELECT id_ord, fornitore, negozio, data_insord, data_prevcons 
        FROM tesord 
        ORDER BY " . $sortby . " " . $ascdesc . " 
        LIMIT :from_record_num, :records_per_page";

需要管理当前是 ASC 还是 DESC 并切换。

感谢您提示一些有效且聪明的方法来实现目标。


解决方案:感谢 Rudy

为了帮助像我这样的其他新手,这是我应用解决方案的方式

// function used in the links
function invdir($dir){ return ($dir == "DESC")? "ASC" : "DESC"; }
// self explaining, it does invert the sort string in the link

// collect, sanitize and default $_GET variables. 
// $ordinaper is the Italian of $sortby
$ordinaper = (isset($_GET['ordinaper']))? (filter_var($_GET["ordinaper"], FILTER_SANITIZE_STRING)) : "id_ord";
$ascdesc = (isset($_GET['ascdesc']))? (filter_var($_GET["ascdesc"], FILTER_SANITIZE_STRING)) : "DESC";
// $filtraforn is a filter/search to show only one provider, see the query, it is assigned with a ìn AJAX live search
$filtraforn = (isset($_GET['filtraforn']))? (filter_var($_GET["filtraforn"], FILTER_SANITIZE_STRING)) : "";

// build the common URL GET part
$getlinks = "&filtraforn=".$filtraforn."&page=".$page."&ascdesc=";
// the variable $page comes from the pagination which is out of the scope. Here I was dealing with the correct management of sorting the HTML table columns

// the query is built accordingly, later is used in a PDO statement
$query = "SELECT id_ord, fornitore, negozio, data_insord, data_prevcons FROM tesord ";
$filtro = (strlen($filtraforn))? "WHERE fornitore = '" . $filtraforn . "' " : "";
$query = $query . $filtro . "ORDER BY ". $ordinaper ." ". $ascdesc ." LIMIT :from_record_num, :records_per_page";
// LIMIT :from_record_num, :records_per_page are bound later with variables coming from the pagination which is out of the scope. Here I was dealing with the correct management of sorting the HTML table columns

// and here it is the final table heading
// the ternary operator (($ordinaper!=="id_ord")? "DESC" : invdir($ascdesc)) is used because when clicking a different column, I want to default the sorting in DESC
echo "<tr>";
    echo "<th></th>";
    echo "<th>". '<a href="read.php?ordinaper=id_ord' .$getlinks. (($ordinaper!=="id_ord")? "DESC" : invdir($ascdesc)) .'">' . "id_ord</th>";
    echo "<th>". '<a href="read.php?ordinaper=ord_evaso' .$getlinks. (($ordinaper!=="ord_evaso")? "DESC" : invdir($ascdesc)) .'">' . "Stato</th>";
    echo "<th>". '<a href="read.php?ordinaper=fornitore' .$getlinks. (($ordinaper!=="fornitore")? "DESC" : invdir($ascdesc)) .'">' . "Fornitore</th>";
    echo "<th>". '<a href="read.php?ordinaper=negozio' .$getlinks. (($ordinaper!=="negozio")? "DESC" : invdir($ascdesc)) .'">' . "Negozio</th>";
    echo "<th>". '<a href="read.php?ordinaper=data_insord' .$getlinks. (($ordinaper!=="data_insord")? "DESC" : invdir($ascdesc)) .'">' . "Data_insord</th>";
    echo "<th>". '<a href="read.php?ordinaper=data_prevcons' .$getlinks. (($ordinaper!=="data_prevcons")? "DESC" : invdir($ascdesc)) .'">' . "Data_prevcons</th>";
    echo "<th>Paia Inev.</th>";
    echo "<th>Azione</th>";
echo "</tr>";

【问题讨论】:

    标签: php html mysql


    【解决方案1】:

    如果您已经发送 排序方式 通过 GET,你为什么不发送 升序、降序 选项也是,我的意思是,您可以使用 1 和 0 而不是实际的 ASC/DESC 并在代码中切换它,例如:

    $ascdesc = ($_GET['ad'])? '0' : '1';
    

    只需将 var 添加到链接中

    echo "<th>".'<a href="showdbtable.php?sortby=negozio&ad='.$ascdesc.'">' . "Negozio</th>";
    

    在你的查询中,类似

     $ascdesc = ($_GET['ad'])? 'asc' : 'desc';
    

    这里非常重要的一点是,如果您通过 GET 接受用户输入,则必须清理 var 以避免 SQL 注入,不要忘记这一点。

    更新:

    使用您自己的代码的可能实现:

    $sortby = (isset($_GET['sortby']))? $_GET['sortby'] : "id_ord";
    $ascdesc = ($_GET['ad']=='asc')? 'ASC' : 'DESC';
    

    查询:

    $query = "SELECT id_ord, fornitore, negozio, data_insord, data_prevcons 
              FROM tesord 
              ORDER BY ".$sortby." ".$ascdesc." 
              LIMIT :from_record_num, :records_per_page";
    

    链接:

    echo "<th>".'<a href="showdbtable.php?sortby=fornitore&ad=<?=(($_GET['ad']=='asc')? 'desc' : 'asc';)?>">' . "Fornitore</th>";
    

    如果您需要添加页面,只需添加当前页面并更改排序,您也可以在链接中添加一个 var

    echo "<th>".'<a href="showdbtable.php?sortby=fornitore&ad=<?php echo (($_GET['ad']=='asc')? 'desc' : 'asc';)?>&page=<?php echo $_GET['page]?>">' . "Fornitore</th>";
    

    还有很多其他方法可以处理分页和排序,但我认为,这可能是一种不麻烦的方法,但是,关于安全性,您可以使用mysql_real_escape

    你也可以通过实现类似this的东西把所有东西留给javascript/jQuery

    希望这可以让您更好地理解,愉快的编码。

    【讨论】:

    • 哎呀,你能解释一下第一行代码吗? $ascdesc = ($_GET['ad'])? '0' : '1'; 。谢谢。
    • 不过,由于分页,我开始觉得解决方案将通过会话或 cookie。
    • 它是一个三元运算符,you can find more examples here,但基本上它是一个 IF,但作为简写,它表示 if($_GET['ad']){ $ascdesc = '0'; }else{ $ascdesc = '1'; } 其中($_GET['ad']) 为真或在这种情况下:1,即使你是使用cookies或者session,逻辑差不多,如果定义了,切换到0,如果是未定义或者0,切换到1
    • 你可能猜到我还是个新手。因此,请允许我向您询问代码的实现示例,因为我无法理解您的代码将如何保留当前的排序顺序并在下次单击时切换它。感谢您的帮助。我的意思是,您的意思是您的方法无需会话或 cookie 即可工作?
    • (继续上一条评论)我还必须处理分页 :-( 。当您单击(例如)第 2 页时,URL 更改为 url?page=2 所以不再排序和 ascdesc。嗯,我也需要解决这个问题。但这让我更加想,我必须将这些变量存储在点击和 url 更改后不会丢失的地方。你会如何处理?
    【解决方案2】:

    在看到这些贴子之前,我想出了一个简单的 ASC/DESC 排序器,只添加了两行代码:

    $SortType = (isset($_GET['SortType']) && $_GET['SortType'] === "DESC") ? "ASC" : "DESC";
    $SortOrder = "ORDER BY `StorageArea` $SortType";
    

    然后我将其附加到查询中(点表示此处未显示的查询的较早部分):

    ... $SortOrder";
    

    链接有:

    <a href="/index.php?SortOrder=StorageArea&SortType=<?=$SortType;?>" id="Sorter_StorageArea">Storage</a>
    

    【讨论】:

      猜你喜欢
      • 2017-10-27
      • 1970-01-01
      • 2015-04-12
      • 2016-05-09
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多