简体   繁体   English

排序列升序/降序:SQL/PHP

[英]Sort Column ASC/DESC: SQL/PHP

There is a mistake causing it not to flip between ASC/DESC and I am 90% sure it is within these few lines.. I think I need to have a "first" loop that will "save" $sort so it flips later.有一个错误导致它不能在 ASC/DESC 之间翻转,我 90% 确定它在这几行之内。我想我需要一个“第一个”循环来“保存” $sort 以便稍后翻转。 But when I do this it causes an array error then.但是当我这样做时,它会导致数组错误。

The SQL is more than likely correct... I am NOT worried about SQL Injections/PDO/security yet as I am adding that code in later. SQL 很可能是正确的......我不担心 SQL 注入/PDO/安全性,因为我稍后会添加该代码。

Full Link: https://solenoidal-slate.000webhostapp.com/完整链接: https : //solenoidal-slate.000webhostapp.com/

if(isset($_GET['sort'])){
$sort = $_GET['sort'];
} else {
    $sort='ASC';
}

$sort == 'DESC' ? $sort ='ASC': $sort='DESC';
$query = "SELECT * FROM employees ORDER BY $order $sort";
$results = mysqli_query($con, $query); ?>

<th><a class="column_sort" id="id" href='?order=id&sort=$sort'>ID<span class="glyphicon glyphicon-sort-by-alphabet"></span></a></th>

This above line is an example of a column that is "Getting" the $sort variable.上面这行是“获取” $sort 变量的列的示例。

EDIT: To save time, $order is not the issue.. Each column name is sorting by column successfully.编辑:为了节省时间,$order 不是问题。每个列名都成功地按列排序。

  if(isset($_GET['order'])){
    $order = $_GET['order'];
    } else {
    $order = 'id';
    } 

Do:做:

$sort = ($sort == 'DESC') ? 'ASC': 'DESC';

and write only one "&" between query string key/value pairs:并且在查询字符串键/值对之间只写一个“&”:

<a ... href="?order=id&sort=$sort">

and apply php code properly in the "href".并在“href”中正确应用php代码。 Otherwise you'll have $sort as html code too.否则,您也会将$sort作为 html 代码。

So, I would do it like so for example:所以,我会这样做,例如:

<?php

$order = isset($_GET['order']) ? $_GET['order'] : 'id';
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'ASC';

$sort = ($sort == 'ASC') ? 'DESC': 'ASC';

$query = "SELECT * FROM employees ORDER BY $order $sort";
$results = mysqli_query($con, $query); ?>
//....
?>

<th>
    <a class="column_sort" id="id" href="?order=id&sort=<?php echo $sort; ?>">
        ID<span class="glyphicon glyphicon-sort-by-alphabet"></span>
    </a>
</th>

Or you could use this directly:或者你可以直接使用它:

<?php

//...
$sort = ((isset($_GET['sort']) ? $_GET['sort'] : 'ASC') == 'ASC') ? 'DESC' : 'ASC';

$query = "SELECT * FROM employees ORDER BY $order $sort";
//...

Answer:回答:

Your final code would be something like this:您的最终代码将是这样的:

if (isset($_GET['order'])) { // Check $_GET['order'] is in the predefined array
    $order = $_GET['order'];
} else {
    $order = 'id';
} 

if(isset($_GET['sort'])) { // Preferably you can check this also in_array($_GET['sort'], ['ASC', DESC])
    $sort = $_GET['sort'];
} else {
    $sort = 'ASC';
}

$query = "SELECT * FROM employees ORDER BY $order $sort";
$results = mysqli_query($con, $query); 

Update:更新:

Your link needs to be changed,您的链接需要更改,

It should be like this http://solenoidal-slate.000webhostapp.com/?order=id&sort=DESC or ASC and remove one & from the link它应该是这样的http://solenoidal-slate.000webhostapp.com/?order=id&sort=DESC或 ASC 并从链接中删除一个&

Now it's like this: https://solenoidal-slate.000webhostapp.com/?order=id&&sort=$sort现在是这样的: https : //solenoidal-slate.000webhostapp.com/?order=id&&sort=$sort

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

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