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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
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.
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.
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 …

829 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