Hi. I'm a real newbie and can't figure this out. I've got this phpcode below for displaying my mySQL database in alphbetical order by last name.
<?php
$db = mysql_connect("localhost", "pstvalu_director", "XXXXXXXXX");
mysql_select_db("pstvalu_directory",$db);
$result = mysql_query("SELECT * FROM alumni ORDER BY lastname",$db);
if (@mysql_num_rows($result))
{
print "<table border=1>\n";
print "<tr>
<td><b>Name</b></td>
<td><b>Class</b></td>
<td><b>Location</b></td>
</tr>\n";
while($row = mysql_fetch_array($result)) {
print "<tr>\n";
print "<td>".$row['lastname'];
if (!empty($row['maidenname'])) print " (".$row['maidenname'].")\n";
print ", ".$row['firstname']."</td>\n";
print "<td>".$row['classyear']."</td>\n";
print "<td>".$row['city'];
if (!empty($row['state'])) print ", \n";
print "".$row['state']."</td>\n";
print "</tr>\n";
}
}
else {
echo "Sorry, no records were found!";
}
print "</table>\n";
?>
You can see it at www.pstvalumni.com/directory It works fine but I want to put in a few links that will reorder the list by other fields when clicked. I used this code:
$default_sort = 'lastname';
if (!isset ($_GET['order']) ||
!in_array ($_GET['order'])) {
$order = $default_sort;
} else {
$order = $_GET['order'];
and this code for creating the link
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=firstname\">Sort By First Name</a>";
These changes work fine only when you do have a value for "?order=" but my problem is that under this example, $order is required and if you don't have any value for it (like when you first load the page) it dies. How do I simply get a value into the $order value (or any other value), while still letting it go to some sort of default value if that value doesn't exist in the address bar? (Sorry if I didn't explain it right but hopefully you know what I'm looking for.)
You can throw away the code I have above since it doesn't work. Just let me know what changes I have to make to the original code (at the top) to get $order only if it exists, and to otherwise go to a default value.
Thanks!