Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Click to Sort" Table Headings

Status
Not open for further replies.

gogirl

MIS
Jun 5, 2002
46
US
I am having problems getting my table to sort by it's column headings. When the data is displayed on the webpage the column headings are linked but they don't do anything when you click on them. Could someone please take a look at my code to see what I am doing wrong.

thx, gogirl

// Connecting to MySQL database
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
print "Connected to MySQL database successfully";

// To read from the database, select it
mysql_select_db("pbx") or die("Could not select database");

print (&quot;<p>Table: &quot; . htmlspecialchars (&quot;pbx&quot;) . &quot;</p>\n&quot;);
print (&quot;<p>Click on a column name to sort the table by that column.</p>\n&quot;);

# Get the name of the column to sort by (optional). If missing, use
# column one. If present, perform simple validation on column name;
# it must consist only of alphanumeric or underscore characters.

$sort_col = get_param_val (&quot;date&quot;); # column name to sort by (optional)
if (!isset ($sort_col))
$sort_col = &quot;1&quot;; # just sort by first column
else if (!ereg (&quot;^[0-9a-zA-Z_]+$&quot;, $sort_col))
die (htmlspecialchars (&quot;Column name $sort_col is invalid&quot;));

# Construct query to select records from the named table, optionally sorting
# by a particular column. Limit output to 50 rows to avoid dumping entire
# contents of large tables.

$query = &quot; SELECT clientmatter,date,time,extention,charge,number,duration,location FROM pbx&quot;;
$query .= &quot; WHERE date = '2002-12-24' AND charge > '0' &quot;;
$query .= &quot; ORDER BY $sort_col&quot;;
$query .= &quot; LIMIT 50&quot;;

$result_id = mysql_query ($query, $link);
if (!$result_id)
die (htmlspecialchars (mysql_error ($link)));

# Display query results as HTML table. Use query metadata to get column
# names, and display names in first row of table as hyperlinks that cause
# the table to be redisplayed, sorted by the corresponding table column.

print (&quot;<table border=\&quot;1\&quot;>\n&quot;);
#@ _HEADER_ROW_
$self_path = get_self_path ();
print (&quot;<tr>\n&quot;);
for ($i = 0; $i < mysql_num_fields ($result_id); $i++)
{
$col_name = mysql_field_name ($result_id, $i);
printf (&quot;<th><a href=\&quot;%s?sort=%s\&quot;>%s</a></th>\n&quot;,
$self_path,
urlencode ($col_name),
htmlspecialchars ($col_name));
}
print (&quot;</tr>\n&quot;);
#@ _HEADER_ROW_
while ($row = mysql_fetch_row ($result_id))
{
print (&quot;<tr>\n&quot;);
for ($i = 0; $i < mysql_num_fields ($result_id); $i++)
{
# encode values, using &nbsp; for empty cells
$val = $row[$i];
if (isset ($val) && $val != &quot;&quot;)
$val = htmlspecialchars ($val);
else
$val = &quot;&nbsp;&quot;;
printf (&quot;<td>%s</td>\n&quot;, $val);
}
print (&quot;</tr>\n&quot;);
}
mysql_free_result ($result_id);
print (&quot;</table>\n&quot;);

mysql_close ($link);

?>
 
I'm not so good reading this stuff without my pretty emacs colors... but I'm fairly certain this is your problem...

$sort_col = get_param_val (&quot;date&quot;);

As I see it your link is

<a href=\&quot;%s?sort=%s\&quot;>%s</a>

hence, you should have something like
$sort_col = @$_GET[&quot;sort&quot;];
if (!(isset($sort_col))) {
set to default;
}

But then again I might be missing something.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top