Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Whats the best way to sort a list / table by ASC or DESC when you have variables in the URL line?

Posted on 2006-11-30
4
Medium Priority
?
302 Views
Last Modified: 2008-03-06
I have a search that returns results, and when you search.. the address bar is like..

http://example.com/?var1=als&var2=sljl&var3=sfjlsa&var4=saflsd


That then pulls the variables, puts them in a MySQL query and returns results in a table depending on how many it finds...

 I want to be able to sort the results by ASC or DESC. Right now I just have it set as Order By Var1 ASC.....



But when I click on Var 2 or Var 3 or whatever, I want it to change what its sorting by... I'm not sure how to tell it what to sort by
without erasing the top line or whatever. Any ideas?
0
Comment
Question by:MattKenefick
4 Comments
 
LVL 5

Accepted Solution

by:
dasmaer earned 252 total points
ID: 18045469
Hi Matt

You can use PHP array sorting methods... providing you first load all the values into an array, see: http://uk.php.net/manual/en/function.sort.php

so if you defined an array as:

$ein_array = array($var1, $var2, $var3, $var4);

you could then:

sort($ein_array);

And output them in alphabetically order. Look through the array sorting stuff at php.net for more functions.

D
0
 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 248 total points
ID: 18045508
You'd have to do something like...

<?php
// Retrieve the variables from $_GET that deal with sorting:
$current_sort_order = ($_GET['current_sort_order']) ? mysql_real_escape_string($_GET['current_sort_order']) : 'default_column';
$current_sort_direction = ($_GET['current_sort_direction']) ? mysql_real_escape_string($_GET['current_sort_direction']) : 'ASC';

$opposite_direction = ($current_sort_direction == 'ASC') ? 'DESC' : 'ASC';


// Compose and run the query:
$query = '...';
$query .= " ORDER BY $current_sort_order $current_sort_direction";


// Then structure the links (presumably in your <th> cells) like so:
?>


<th><a href="...?sort_order=var1&amp;sort_direction=<?= ($current_sort_order == 'var1') ? $opposite_direction : 'ASC') ?>">Sort by var1</a></th>

<th><a href="...?sort_order=var2&amp;sort_direction=<?= ($current_sort_order == 'var2') ? $opposite_direction : 'ASC') ?>">Sort by var2</a></th>



I've left out some nice cosmetic things like little arrows to show which way it's currently sorted, and just focused on the functionality to get it to sort.

Yes, I know that's a lot of ternary operators for fewer than ten lines of code.  (-:
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question