Solved

Sort onclick, resort onclick

Posted on 2013-12-13
3
632 Views
Last Modified: 2013-12-13
I have the following code that organizes a table of information.  You can click the heading of each row and that row will be ordered Alphabetically ASC.  I want to add to the functionality.  The first time you click, the row is ordered ASC.  If you click it again, It is ordered DESC.  How do I do that?

$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'an';		
		// Determine the sorting order:
switch ($sort) {
	case 'an':
		$order_by = 'asset_name ASC';
		break;
	case 'at':
		$order_by = 'asset_type ASC';
		break;	
	case 'ad':
		$order_by = 'asset_desc ASC';
		break;
	case 'ph':

	default:
		$order_by = 'asset_name ASC';
		$sort = 'an';
		break;
}


		$sql = "SELECT asset_id, asset_name, asset_type, asset_desc, color
				FROM assets
				ORDER BY $order_by";
		$asset = $pdo->prepare($sql);		

// Table header:
echo '<fieldset><table align="left" cellspacing="0" cellpadding="5" width="95%" class="content">
<tr>
	<td align="left"><b>Delete</b></td>
	<td align="left"><b>Edit</b></td>
	<td align="left"><b><a href="view_assets.php?sort=an">Asset Name</a></b></td>
	<td align="left"><b><a href="view_assets.php?sort=at">Asset Type</a></b></td>
	<td align="left"><b><a href="view_assets.php?sort=ad">Asset Description</a></b></td>
</tr>
';

Open in new window

0
Comment
Question by:rgranlund
  • 2
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39717934
There are several ways to do this.  You could remember the previous sort order in the session, but a more user-friendly design might allow the client to choose whether to sort ASC or DESC.  You can use a pair of links in the column header to indicate the sort order.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39717971
This is an excerpt from a client script. I do not have a teaching example yet, but maybe you can follow the design pattern.  The URL has an argument "o=" that tells the sorting order.

// THE LIST ALLOWS FOR SORTING
$url = $_SERVER['PHP_SELF'];
$sig = !empty($_GET['o']) ? $_GET['o'] : NULL;
switch (strtolower($sig))
{
    case 'ia':
        $sql = 'SELECT * FROM payments ORDER BY invoice_number, txn_id';
        break;
    case 'id':
        $sql = 'SELECT * FROM payments ORDER BY invoice_number DESC, txn_id ASC';
        break;
    case 'ta':
        $sql = 'SELECT * FROM payments ORDER BY txn_id, invoice_number';
        break;
    case 'td':
        $sql = 'SELECT * FROM payments ORDER BY txn_id DESC, invoice_number ASC';
        break;
    default:
        $sql = 'SELECT * FROM payments ORDER BY invoice_number, txn_id';
        break;
}
if (!$res = $mysqli->query($sql))
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}
$out = <<<EOD
<table>
<tr>
<th class="pid">Pmt Id</th>
<th class="pin"><a href="$url?o=ia">&#8657;</a> Invoice Number <a href="$url?o=id">&#8659;</a></th>
<th class="tin"><a href="$url?o=ta">&#8657;</a> Transaction Id <a href="$url?o=td">&#8659;</a></th>
<th class="amt">Amount</th>
</tr>
EOD;

Open in new window

0
 
LVL 7

Author Closing Comment

by:rgranlund
ID: 39717983
It makes sense to add directional arrows.  The second example is not as clear "why".
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:

863 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

26 Experts available now in Live!

Get 1:1 Help Now