編輯:從接受的答案HTML表的MySQL PHP MySQL的切換上排序的列標題順序ASC DESC點擊
我問這裏的暗示是最聰明的方法添加的解決方案。
需要先說:我懇請您避免暗示databales和類似。
我在HTML表格中顯示MySQL表格內容。
我認爲是showdbtable.php
它使用包括echohtmltable.php
查詢數據庫和回顯錶行。
所以在echohtmltable.php
目前有涉及這個問題
GET變量檢查
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>";
哎喲,你能不能解釋第一行代碼? '$ ascdesc =($ _GET ['ad'])? '0':'1';'。謝謝。 – Robert
雖然,由於分頁,我開始覺得解決方案將通過會話或cookie。 – Robert
這是一個三元運算符,[你可以在這裏找到更多的例子](https://davidwalsh.name/php-shorthand-if-else-ternary-operators),但基本上它是一個IF,但作爲速記,它的意思是' if($ _ GET ['ad']){$ ascdesc ='0'; } else {$ ascdesc ='1'; }(其中'($ _GET ['ad'])'爲true或在這種情況下爲1,即使您使用的是cookie或會話,其邏輯幾乎相同,如果定義了它,則切換爲0是未定義的或0,切換到1 –