Solved

Sort onclick, resort onclick

Posted on 2013-12-13
3
653 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 110

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 110

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

733 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