HTML表的MySQL PHP MySQL的切换上排序的列标题顺序ASC DESC点击

问题描述:

编辑:从接受的答案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>"; 

如果您已经发送 通过GET来排序 ,为什么不发送 ASC,DESC 选项也是,我的意思是,您可以使用1和0代替实际的ASC/DESC,并在您的c中切换它颂,例如:

$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注入,不要忘记这一点。

UPDATE:

可能实现用自己的代码:

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

如果你需要添加的页面,只需添加当前页面和排序改变,你还可以添加一个变种的链接

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

还有许多其他的方式来处理分页和排序,但我认为,没有进入很多麻烦,这可能是一个办法,但是,关于安全性,您可以通过实现类似this

使用mysql_real_escape

你也可以把一切的JavaScript/jQuery的希望这可以让你更好理解,开心编码。

+0

哎哟,你能不能解释第一行代码? '$ ascdesc =($ _GET ['ad'])? '0':'1';'。谢谢。 – Robert

+0

虽然,由于分页,我开始觉得解决方案将通过会话或cookie。 – Robert

+0

这是一个三元运算符,[你可以在这里找到更多的例子](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 –