Koşullara göre bir liste filtreleme

0 Cevap php

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)

Ve kullanıcılar bir ekip aittir. Ön ucunda ben onay kutularını var:

[ ] 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.

Benim sonuçları filtrelemek için onay kutularını herhangi bir kombinasyonunu kullanabilirsiniz.

The (ugly) solution:

Bir int (küçük sonlu) ikili sayı olarak bayrakları dönüştürmek ve kullanmak 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?

0 Cevap