简体   繁体   English

HTML TABLE PHP MySQL在列标题上切换MySQL排序顺序ASC DESC

[英]HTML TABLE PHP MySQL toggle MySQL sorting order ASC DESC on column header click

EDIT : added solution from the accepted answer 编辑 :从接受的答案中添加解决方案

The hint I'm asking here is the smartest method for. 我要问的提示是最聪明的方法。

Need to be said first: I kindly ask you to avoid suggesting databales and similar. 首先需要说:我恳请您避免提出数据平衡或类似问题。

I'm displaying a MySQL table content in a HTML table. 我在HTML表中显示MySQL表的内容。

I have the view that is showdbtable.php which uses as include echohtmltable.php which queries the database and echoes the table rows. 我有一个视图是showdbtable.php ,它使用作为include echohtmltable.php来查询数据库并回显表行。

So in echohtmltable.php currently there is quite everything involved in this question 因此,在echohtmltable.php目前这个问题涉及的一切

GET variable check GET变量检查

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

and the consequent query 和随之而来的查询

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

which next is used to echo the table rows creating also the sort mechanism in the columns headers 接下来用于回显表行,并在列标题中创建排序机制

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>";

as you know this first part works. 如您所知,第一部分有效。

When I click one of the above links, the query works but currently as obvious from the code, in DESC only. 当我单击以上链接之一时,该查询有效,但当前从代码中可见,仅在DESC中。

The kind request is a suggestion altogether. 善意的要求一共是一个建议。 But PLEASE also NOTE that I have pagination, you see, LIMIT :from_record_num, :records_per_page , that should be taken in consideration. 但也请注意,我有分页, LIMIT :from_record_num, :records_per_page ,应予以考虑。

Which is the smartest and efficient way to toggle between ASC and DESC in such a way that after having clicked one link, eg "Negozio", clicking again "Negozio" it goes sorted in ASC, and next click it will toggle DESC, and next click ASC and so on. 这是在ASC和DESC之间进行切换的最聪明,最有效的方法,即单击一个链接(例如“ Negozio”),再次单击“ Negozio”后,它将在ASC中进行排序,然后单击它将切换DESC,然后单击下一步单击ASC,依此类推。

I'm sure the query will mutate in having a variable I may call $ascdesc 我确定查询会因为有一个我可能称为$ ascdesc的变量而发生变化

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

need to manage if currently is ASC or DESC and toggle. 需要管理当前是ASC还是DESC并进行切换。

Thank you for hinting on some efficient and smart method to achieve the goal. 感谢您暗示一些有效且明智的方法来实现该目标。


Solution : thank you to Rudy 解决方案 :谢谢Rudy

For the sake of helping others newbies like me, here is how I have applied the solution 为了帮助其他像我这样的新手,以下是我应用解决方案的方式

 // 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>"; 

If you are already sending the sortby via GET, why wouldn't you send the ASC,DESC Option too, I mean, you could use 1 and 0 instead of the actual ASC/DESC and toggle it inside your code,for example: 如果您已经通过GET发送了sortby,为什么还不发送ASC,DESC Option,我的意思是,您可以使用1和0而不是实际的ASC / DESC并在代码内进行切换,例如:

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

And just add the var to the link 只需将var添加到链接

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

And in you query, something like 在您查询时,类似

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

Something very important here is that if you are accepting user input via GET, you have to sanitize the var to avoid SQL injections, don't forget this. 这里非常重要的一点是,如果要通过GET接受用户输入,则必须清理var以避免SQL注入,请不要忘记这一点。

UPDATE: 更新:

Possible implementation with your own code: 用您自己的代码可能的实现:

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

The Query: 查询:

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

The link: 链接:

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

And if you need to add the page, just add the current page and change sort, you can add a var also to the link 如果您需要添加页面,只需添加当前页面并更改排序,就可以在链接中添加变量

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

There are many other ways to handle the pagination and sorting, but I think that, without getting into a lot of trouble, this could be a way, however, about the security, you can use mysql_real_escape 还有许多其他方法可以处理分页和排序,但是我认为这可能是一种安全的方法,但是不必担心麻烦,可以使用mysql_real_escape

You could also leave everything to javascript/jQuery by implementing something like this 您也可以通过实施类似放下一切的JavaScript / jQuery的

Hope this can give you a better understanding, happy coding. 希望这可以给您更好的理解,编码愉快。

Before seeing these postings, I came up with a simple ASC/DESC sorter that added only two lines of code: 在看到这些帖子之前,我想出了一个简单的ASC / DESC分类器,仅添加了两行代码:

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

then I appended this to the query (the dots are indicating the earlier part of the query that is not shown here): 然后将其附加到查询中(点表示此处未显示的查询的较早部分):

... $SortOrder";

and the link has: 链接具有:

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

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

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