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
259 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 63 total points
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now