sorting columns in a client-side page
- From: Greg Russell <me@xxxxxxxxxxx>
- Date: 30 Dec 2008 01:14:04 GMT
Greetings,
What is an easy way to produce the headings for columns in an html table
that are sortable by clicking on them please?
I see these quite often and find them to be very useful, yet the code to
produce them is elusive for me. I am a beginner at all of php, mysql and
html and know nothing of css, and hope to find some coding examples that
are relatively easy to understand and incorporate into the existing
elementary-level code below.
Given a ./dbinfo.inc.php that contains $username, $password, $database
and $table, the following is generic portable code that *should* work for
anybody to see the very simple kind of html tables that I'm currently
working with (my tables exclude the first field which is a unique auto-
increment key), but of course the "Chg" and "Del" radio buttons won't
work for anybody without the other scripts referenced in the onclick
events, so its perfectly safe for anyone to use on an existing database:
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password)
or die( "Unable to connect to database");
mysql_select_db($database)
or die( "Unable to select database");
$querycols="SHOW COLUMNS IN $table;";
$queryrows="SELECT * FROM $table;";
$resultcols=mysql_query($querycols);
$resultrows=mysql_query($queryrows);
$numcols=mysql_numrows($resultcols);
$numrows=mysql_numrows($resultrows);
mysql_close();
echo "<b><center>Database Output: $numcols columns x $numrows rows</
center></b>\n";
// build the table column headers
echo "<table border=\"2\" cellspacing=\"2\" cellpadding=\"2\">\n<tr>
\n<th><font face=\"Arial, Helvetica, sans-serif\">Chg</font></th>
\n<th><font face=\"Arial, Helvetica, sans-serif\">Del</font></th>\n";
$i=0;
while ($i < $numcols) {
$col_name[$i] = mysql_result($resultcols, $i, "Field");
if ($i > 0) echo "<th><font face=\"Arial, Helvetica, sans-serif\">
$col_name[$i]</font></th>\n";
++$i;
}
// build the rows
$j=0;
while ($j < $numrows) { //begin each row
$id = mysql_result($resultrows, $j, "id");
echo "<tr>\n";
echo "<td align=center><input type=\"radio\" name=\"update\" value=
\"$id\" onclick=\"location.href='./update.php?ud_id='+this.value;\"</td>
\n";
echo "<td align=center><INPUT TYPE=\"radio\" NAME=\"delete\" VALUE=
\"$id\" onclick=\"if (confirm('Delete this record?\\nThis will be
permanent.')) {location.href='./delete.php?ud_id='+this.value}\"</td>\n";
$i=1;
while ($i < $numcols) { // begin columns
$item = mysql_result($resultrows, $j, "$col_name[$i]");
echo "<td>$item </td>";
++$i;
} //end of columns
echo "\n</tr>\n";
++$j;
} // end of rows
echo "</table><br>\n";
?>
.
- Follow-Ups:
- Re: sorting columns in a client-side page
- From: Gordon Burditt
- Re: sorting columns in a client-side page
- From: Jerry Stuckle
- Re: sorting columns in a client-side page
- Prev by Date: Re: ldap_bind issues
- Next by Date: Re: Strings, arrays and efficiency
- Previous by thread: identify $handles in $resource ?
- Next by thread: Re: sorting columns in a client-side page
- Index(es):