Solved

Sort onclick, resort onclick

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article discusses four methods for overlaying images in a container on a web page
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.

746 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

9 Experts available now in Live!

Get 1:1 Help Now