Solved

Sort onclick, resort onclick

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
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.

785 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