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
281 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 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