What are some best practices for handling ASC and DESC sorting in PHP when clicking on a link multiple times?

When handling ASC and DESC sorting in PHP when clicking on a link multiple times, it is important to keep track of the current sorting order and toggle between ASC and DESC accordingly. One way to achieve this is by using a session variable to store the current sorting order and updating it each time the link is clicked.

session_start();

$sortOrder = isset($_SESSION['sortOrder']) ? $_SESSION['sortOrder'] : 'ASC';

if ($sortOrder == 'ASC') {
    // Sort in ascending order
    $sortOrder = 'DESC';
} else {
    // Sort in descending order
    $sortOrder = 'ASC';
}

$_SESSION['sortOrder'] = $sortOrder;

// Use $sortOrder in your SQL query to apply the sorting order