Solved

Sort onclick, resort onclick

Posted on 2013-12-13
3
642 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 109

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 109

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

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

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

792 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