【发布时间】: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>";
【问题讨论】: