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
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 5

Accepted Solution

by:
dasmaer earned 63 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 62 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 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