Reputation: 272
The title might not explain my problem fully but I wasn't sure how to in a title so I will explain more here.
I have a table which holds various information on bands/acts and I have a browse page which shows all the acts in a certain area etc. Each act can say whether they want drinks/food/accommodation/extra/travel to be paid for. On the browse page I also have a sort function which I want to be able to sort the results by which acts want what paid.
There are five check boxes for each of the options but when I check only travel for example, acts which have travel as well as the others ticked are on top when I want acts which only want travel to be paid for on top.
Currently the sort function starts with the original query from when browsing:
SELECT * FROM acts
And then the sort function appends the ORDER BY statement depending on what has been checked:
SELECT * FROM acts ORDER BY travel DESC
Or if more than one was selected:
SELECT * FROM acts ORDER BY travel DESC, food DESC
But as I said this doesn't order the list properly (so for the second order by query I'd want an act which only wanted travel and food at the top).
I've also tried adding in ORDER BY ASC for the options which weren't selected but this didn't help.
Also the columns in the database hold either "yes" or "no" which is why I've used DESC when I want them to be on top. I didn't make the database and it can't be changed either.
Thanks for any help.
EDIT: you can see what I mean here: http://www.acthook.com/browse.php If you leave the browse boxes blank and click browse to get all results, then tick travel and click sort, there is an act wanting only travel part way down the first page.
Upvotes: 1
Views: 3948
Reputation: 4528
Oh I see, thanks for the example, it wasn't that clear without :) Then you should have something like this:
SELECT * FROM acts ORDER BY travel DESC, food ASC, accomodation ASC, drink ASC, extra_fee ASC;
And if the user clicks on food too:
SELECT * FROM acts ORDER BY travel DESC, food ASC, accomodation ASC, drink ASC, extra_fee ASC;
Or if he clicks only on food:
SELECT * FROM acts ORDER BY food DESC, travel ASC, accomodation ASC, drink ASC, extra_fee ASC;
As you can see here, the order of the fields in the order clause have been changed. You'll have to pay attention to that too.
In order to build the query, you can do something like this :
$allowedOrderBy = array('travel','food','dring','extra_fee');//etc
$foundOrderBy = array();
foreach ($_POST as $orderBy) {
if (in_array($orderBy, $allowedOrderBy)) {
$foundOrderBy[] = $orderBy;
}
}
$notFoundOrderBy = array_diff($allowedOrderBy, $foundOrderBy);
$orderByStr = implode(' DESC,', $foundOrderBy).' DESC, ';
$orderByStr.= implode(' ASC,', $notFoundOrderBy). 'ASC';
Upvotes: 0
Reputation: 61
I'm not sure if it's right idea (problem is in a database structure), but try something like this>
check only travel:
SELECT * FROM table
WHERE travel = yes
and food = no
and .. = no
UNION SELECT * FROM table
EDIT: or maybe something like SELECT * FROM order by travel, food DESC
Upvotes: 1