This morning I've been working on the member index page that simply shows a list of registered members on my site. Locally I have error_reporting set to E_ALL as I like forcing myself to code to a higher standard whenever possible. One of stricter warnings with this error mode are undefined variables (e.g. $_GET['something'] is not defined). I was trying to figure out how to get the order URL's to work without generating any PHP warnings and then I realized instead of having the URL look something like members/?column=alias&sort=desc I could simply use members/?alias&desc. This meant instead of first determining if the value was set and then what it was set to...and still having to repeat the same steps (since there are multiple columns to sort by) all I had to do was execute a simple isset to determine if I should echo anything special out.
Here is some example code not yet ready for a live environment though it should give an idea of how simple this approach is. Suggestions for improvement are of course welcomed!
Code: Select all
echo '<thead>'."\n";
echo '<tr><td><a class="block" href="members/';
if (!isset($_GET['alias'])) {echo '?alias&desc';}
echo '" rel="nofollow" tabindex="3" title="">Alias</a></td><td><span>Location</span></td><td><a class="block" href="members/';
if (isset($_GET['posts'])) {if (!isset($_GET['desc'])) {echo '?posts&desc';} else {echo '?posts';}} else {echo '?posts';}
echo '" rel="nofollow" tabindex="3" title="">Posts</a></td><td><a class="block" href="members/';
if (isset($_GET['rank'])) {if (!isset($_GET['desc'])) {echo '?rank&desc';} else {echo '?rank';}} else {echo '?rank';}
echo '" rel="nofollow" tabindex="3" title="">Rank</a></td><td><a class="block" href="members/';
if (isset($_GET['registered'])) {if (!isset($_GET['desc'])) {echo '?registered&desc';} else {echo '?registered';}} else {echo '?registered';}
echo '" rel="nofollow" tabindex="3" title="">Registered</a></td></tr>'."\n";
echo '</thead>'."\n";