Solved

Sort a table

Posted on 2009-04-10
6
268 Views
Last Modified: 2012-05-06
I'm using PHP to pull out a table from mysql nad spit it into the browser.

Is it possible for me to make the table sortable by the user clicking on the column headers?

See the code I'm using below.
// Execute the query

if ($result = $mysqli->query($query)) {
 

printf("<style type='text/css'>");

printf("table.sample {");

printf("border-collapse: collapse;");

printf("border-style: dotted dotted dotted dotted;");

printf("margin-left:auto; margin-right:auto;");

printf("body.sample {");

printf("text-align:center;");

printf("}");

printf("table.sample th {");

printf("border-width: 1px 1px 1px 1px;");	

printf("padding: 3px 3px 3px 3px;");

printf("border-style: dotted dotted dotted dotted;");

printf("}");

printf("table.sample td {");

printf("padding: 3px 3px 3px 3px;");

printf("border-style: dotted dotted dotted dotted;");

printf("border-width: 1px 1px 1px 1px;");

printf("}");

printf("</style>");
 
 

printf("<body class='sample'>");

printf("<table class='sample' border='1'>");

printf("<tr>");
 

// Output the headers

$fields = $result->fetch_fields();

foreach ($fields as $field)
 

printf("<th align='left'>%s</th>", $field->name);

printf("</tr>");
 
 

// Output the results

while ($table = $result->fetch_row()) {

$field1 = $table[0];

$field2 = $table[1];

$field3 = $table[2];

$field4 = $table[3];

$field5 = $table[4];

$field6 = $table[5];

//$field7 = $table[6];

//$field8 = $table[7];

//$field9 = $table[8];

//$field10 = $table[9];
 

printf("<tr>");

printf("<td>%s</td><td>%s</td>", $field1, $field2);

printf("<td>%s</td><td>%s</td>", $field3, $field4);

printf("<td>%s</td><td>%s</td>", $field5, $field6);

//printf("<td>%s</td><td>%s</td>", $field7, $field8);

//printf("<td>%s</td><td>%s</td>", $field9, $field10);
 

printf("</tr>");

}

printf("</TABLE>");

printf("<p>");
 

}

Open in new window

0
Comment
Question by:lee_jd
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:LinuxNubb
ID: 24116876
This is absolutely possible, but not easy to implement.  You will need to keep track of the current column you are sorting, and also the direction you are sorting it.  Once the page reloads, you'll need to check if the the column is already being sorted, and if it is then sort it the other way.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24117794
Here is some sample code showing how to use the GET string in the URL to signal which ORDER BY clause to use in the query.  Look it over and post back here if you have any questions. Best, ~Ray
<?php 

require_once('config.php');
 

// ANALYZE GET STRING FOR THE ORDER BY CLAUSE

switch ($_GET[q])

{

   case "a": $orderby = 'ORDER BY s_aname, s_url';                 break;

   case "t": $orderby = 'ORDER BY s_city, s_camp, s_url';          break;

   case "c": $orderby = 'ORDER BY s_camp, s_state, s_url';         break;

   case "l": $orderby = 'ORDER BY s_lastcall, s_url';              break;

   case "s": $orderby = 'ORDER BY s_state, s_city, s_camp, s_url'; break;

   default:  $orderby = 'ORDER BY s_url, s_camp';

}
 
 

// THE QUERY HERE

$ssql = "SELECT * FROM {$my_dbt_prefix}_PROSPECTS WHERE s_source = \"XYZ\" ";

$ssql .= "AND (s_state = \"PA\"

            OR s_state = \"MD\"

            OR s_state = \"VA\"

            OR s_state = \"DE\"

            OR s_state = \"WV\"

            OR s_state = \"NJ\"

            OR s_state = \"NC\"

            OR s_state = \"SC\")
 

            AND (s_id <> \"OK\")

            AND (s_id <> \"DEAD\")

            $orderby ";
 
 

// HERE IS THE CODE TO GIVE SORTING OPTIONS:

echo "<a name=\"top\"></a>\n";

echo "<p>";

echo "<a href=\"$PHP_SELF?q=a\">DIRNAME</a> | ";

echo "<a href=\"$PHP_SELF?q=c\">CAMP</a> | ";

echo "<a href=\"$PHP_SELF?q=l\">LASTCALL</a> | ";

echo "<a href=\"$PHP_SELF?q=t\">CITY</a> | ";

echo "<a href=\"$PHP_SELF?q=s\">STATE</a>  ";

echo "</p>\n";

Open in new window

0
 
LVL 2

Author Comment

by:lee_jd
ID: 24119126
This is great.   One other thing.  What is the SQL to sort in reverse order?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:lee_jd
ID: 24119520
It's okay I found it DESC.

Thanks for your help.
0
 
LVL 2

Author Closing Comment

by:lee_jd
ID: 31568956
Top class answer - very helpful.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24120117
Thanks for the points; it's a great question, and regarding this: "It's okay I found it DESC." -- that is exactly right.  You can mix the ORDER BY statement, too, for example, to see the largest and most recent sales in each state, you might use something like this: "ORDER BY sales_date DESC, sales_price DESC, sales_state ASC"  The default is ASC for each field in ORDER BY.

Best, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

930 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