Advertisement

05.16.2008 at 04:57PM PDT, ID: 23410028
[x]
Attachment Details

Getting DB Table In HTML Page to Sort By Column

Asked by macrolific in PHP Scripting Language, Hypertext Markup Language (HTML), Extensible HTML (XHTML)

Tags: PHP, MySQL, HTML

Below is a beautiful little PHP script that someone here at EE turned me on to to display my MySQL database table on a Web page in an html table.

What I need now is to figure out how to enable sorting by clicking on the header of each column.  Can anyone tell me how to do this, or at least point me in the right direction?

Thanks.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
<?php
 
if (!$link = mysql_connect('server_name_or_IP', 'username', 'password')) {
    echo 'Could not connect to mysql';
    exit;
}
if (!mysql_select_db('mysql_dbName', $link)) {
    echo 'Could not select database';
    exit;
}
$sql    = 'SELECT * FROM myTable';
$result = mysql_query($sql, $link);
 
if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
print "<table align=\"center\" border=\"0\" cellpadding=\"2\" bgcolor=\"#4F5158\">
           <tr style=\"color: white;\">
           <th>First Name</th>
           <th>Last Name</th>
           <th>Address</th>
           <th>City</th>
           <th>State</th>
           <th>Zip</th>  
           <th>Country</th>
           <th>Phone</th></tr>";
           
print "<tr><td></td></tr>";
 
// Define colors for alternating rows
$color1 = "#fffaf0";
$color2 = "#ffffff";
$row_count = 0;
 
while ($row = mysql_fetch_row($result))
{
$row_color = ($row_count % 2) ? $color1 : $color2;
 
     print "<tr>";
     foreach ($row as $field)
     {
           print "<td nowrap=\"nowrap\" align=\"center\" bgcolor=\"$row_color\"><font size=\"2\">$field</font></td>";
     }
     print "</tr>";
     
          $row_count++;
}
print "</table>";
 
?>
 
 
[+][-]05.16.2008 at 05:21PM PDT, ID: 21587488

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.16.2008 at 05:59PM PDT, ID: 21587555

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.17.2008 at 03:31AM PDT, ID: 21588644

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.17.2008 at 09:18AM PDT, ID: 21589922

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.17.2008 at 09:43AM PDT, ID: 21589983

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.17.2008 at 12:53PM PDT, ID: 21590563

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.17.2008 at 01:25PM PDT, ID: 21590647

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP Scripting Language, Hypertext Markup Language (HTML), Extensible HTML (XHTML)
Tags: PHP, MySQL, HTML
Sign Up Now!
Solution Provided By: macrolific
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.17.2008 at 02:13PM PDT, ID: 21590766

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628