<? include("connection.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?
$searchquery = ""; //if you intend to add search to this page
// columns that you can order by
$orderbycols = array();
$orderbycols["fname"] = "first name";
$orderbycols["lname"] = "last name";
$orderbycols["phone"] = "phone";
$orderbycols["username"] = "user name";
$orderbycols["address"] = "address";
// columns that are dificult to order by
$nonorderbycols = array();
$nonorderbycols["hobbies"] = "favorite hobbies";
$nonorderbycols["signature"] = "signature";
// merge the column names for the query
$querysel = array_merge($orderbycols, $nonorderbycols);
// declare order by variable
$orderby = "";
if(isset($_GET['order']) && in_array(strtolower($_GET['order']), array_keys($orderbycols)))
{
$orderby .= " order by '" . strtolower($_GET['order']) . "'";
// if we are ordering by desc values
if(isset($_GET['desc']) && $_GET['desc'] == "true")
{
$orderby .= " desc";
}
}
// main query
$query = "select " . implode(", ", array_keys($querysel)) . " from table inner join other_table using(colname_pk)" . $searchquery . $orderby;
$result = mysql_query($query);
?>
<body>
<table>
<tr>
<?
// columns that can be ordered
foreach($orderbycols as $key=>$value)
{
?>
<td><?
$hreforder = "";
if(trim($orderby) != "" && strtolower($_GET['order']) == $key)
{
$hreforder = isset($_GET['desc']) && $_GET['desc'] == "true"?"":"&desc=true";
}
?><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?order=<? echo $key . $hreforder; ?>"><? echo ucwords($value); ?></a></td>
<?
}
?>
<?
// columns that cannot be ordered
foreach($nonorderbycols as $key=>$value)
{
?>
<td><? echo ucwords($value); ?></td>
<?
}
?>
</tr>
<?
// display database loop here
?>
</table>
</body>
</html>
<?
mysql_close();
?>