The problem
I have a list of tasks with which a user is greeted with upon login. I want to be able to filter tasks according to assignment. The relevant fields on the table are:
**tasks table**
task_id | (FK int) user | (FK int) team
(assigned to) (assigned to)
And users belong to one team. On the front end I have checkboxes:
[ ] Mine [ ] My Team's [ ] Others (Filter)
The conditions are mutually exclusive. For example, if I only check "My Team's", it's implied that I want to see all tasks assigned to my team but not me. If I check "Mine", I only want to see tasks assigned to me.
I can use any combination of the checkboxes to filter my results.
The (ugly) solution:
Convert flags as binary number to an int
(little endian) and use switch
:
$num = bindec("{$flag1}{$flag2}{$flag3}");
switch ($num) {
// [ ] Mine [ ] My Team's [X] Others
case 1:
$filter = array('team <>' => $teamId);
break;
// [ ] Mine [X] My Team's [X] Others
case 3:
$filter = array('user <>' => $userId);
break;
/* a few more */
// [X] Mine [X] My Team's [X] Others
default:
$filter = array();
break;
}
The generated $filter
array is passed on to a query builder.
It works. But I feel this is really ugly. Is there a better way?