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.
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.
Comments