HA! Take THAT MySQL!
I’ve learned something new! MUAHAHAHA!
I’ve modified the search resultsof my library database search so that for most search results, you can sort the results by clicking on the heading.
If you aren’t a geek, you’ll want to go and play with the search, rather than read than read the code bits that follow.
I got that using “switch”
if(isset($_GET[‘sort’]))
{
switch($_GET[‘sort’])
{
case ‘a’:
$order_by = LastName ASC, FirstName, Series, Year’;
$a = “{$_SERVER[‘PHP_SELF’]}?sort=b”;
break;case ‘b’:
$order_by = ‘LastName DESC, FirstName, Series, Year’;
$a = “{$_SERVER[‘PHP_SELF’]}?sort=a”;
break;default:
$order_by = ‘LastName, FirstName, Series, Year’;
$sort = ‘a’;
break;
}$sort = $_GET[‘sort’];
} else {
$order_by = ‘LastName, FirstName, Series, Year’;
$sort = ‘a’;
}
Isn’t that nice?
But even better, I managed to get a more accurate sort for my “title” results. I got it to ignore a, an, and the when they start a book title. Which means the title results are now truly alphabetical.
ORDER BY trim(LEADING ‘The ‘ FROM trim(LEADING ‘A ‘ FROM trim(LEADING ‘An ‘ FROM Title))) DESC
Isn’t that lovely?
You wouldn’t believe how long it took me to find that. Most of what I came across wanted me to modify the book title to place the definite articles at the end of the title followed by a comma, or in a separate column, or even create a separate column for sorting, all of which were overkill for what I wanted.
But trim(LEADING ‘The ‘ FROM trim(LEADING ‘A ‘ FROM trim(LEADING ‘An ‘ FROM X))) DESC worked like a charm.