ASC DESC in the same link

Mar
if (isset($_GET['order']) && $_GET['order'] == 'category')
{

    $sql .= " ORDER BY category ".$_GET["direction"];
}

<a href='?order=category&direction=ASC'>

When the user clicks second time on the link I want to order the table in DESC order. I have the a-z order I need to reverse them when the user clicks on the same link.

Ketan Yekale

Simply check what is contained in the $_GET['direction'] and reverse it each time the submit is run.

Also, to take care of page reloads, save the value of previous direction in session.

$dir = 'DESC'; // set default for first execution
@session_start(); // start the session if it isn't already started.
if(!empty($_GET['direction'])){
  $dir = $_GET['direction'] == 'ASC' ? 'ASC' : 'DESC'; // avoids SQL injection.
  $_SESSION['direction'] = $dir;
}elseif(!empty($_SESSION['direction'])){
  $dir = $_SESSION['direction'];
 }
if (isset($_GET['order']) && $_GET['order'] == 'category') {
    $sql .= " ORDER BY category ".$dir;
}

$reverseDir= $dir =='DESC' ? 'ASC' : 'DESC'; //reverses the direction to be used in the link
<a href='?order=category&direction='<?php echo $reverseDir;?>>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sort Asc and desc in the same column

From Dev

How to sort a same column both in asc order and desc order

From Dev

How to sort a same column both in asc order and desc order

From Dev

dynamic asc desc sort

From Dev

SQL Dynamic ASC and DESC

From Dev

Should first_value() over (order by something asc) be the same as last_value () over (order by something desc)?

From Dev

conditional asc or desc in in a order byexpression

From Dev

Eloquent sortBy - Specify asc or desc

From Dev

Access Vba Setorderby ASC or DESC with and if

From Dev

conditional asc or desc in in a order byexpression

From Dev

Order by in mysql without asc and desc

From Dev

Implementing sort ASC or DESC in Rails

From Dev

Sorting div asc desc by content

From Dev

OrderBy based on list of fields and Asc / Desc rules

From Dev

SQL: Order by column, then by substring mix asc and desc

From Dev

Datastax Driver Mapping ORDER BY ASC/DESC

From Java

Room DAO Order By ASC or DESC variable

From Dev

Order table column in asc/desc order

From Dev

Case statement for Order By clause with Desc/Asc sort

From Dev

Order By Asc and Desc both in single query

From Dev

is there a way to choose the sorting of ksort? like ASC , DESC

From Dev

Lodash - sort objects in object ( desc and asc)

From Dev

PHP Mysql ASC AND DESC Order Proper use?

From Dev

We get data with ORDER BY ASC but NOT BY DESC

From Dev

Finding asc/desc sequences in Pandas dataframe

From Dev

Custom Comparator implementation for both ASC and DESC order

From Dev

how to order by <= 7 desc then by > 7 asc?

From Dev

Jquery Isotope - sorting ASC DESC - strange behaviour

From Dev

PHP Mysql ASC AND DESC Order Proper use?

Related Related

HotTag

Archive