1

I m working on a filter and sort option for a website and seem to be stuck. The whole code can be seen here: http://pastebin.com/pYfnLiSZ

It's not 100% complete ... parts like this

        if(array_key_exists('number', $sorts))
        {
            $issue = $sorts['number'];

            $setIssue = "AND i.item_number = ?";
        }

still need to be edited a bit, but I hope it's understandable.

The part that I can't figure out is this(line 126 in pastebin):

if( !empty($userIDS) && $letter )
        {
            $ref = array();

            foreach($userIDS as $id)
            {
                $ref[] = $id['followed'];
            }

            $ref[] = $letter;

            $params = implode(",", array_fill(0, count($userIDS), "?"))."?";
            $prep = implode("", array_fill(0, count($userIDS), "i"))."s";

        }

I'm not sure how to make the above if dynamic. I only need it if $userIDS array is not empty and either $letter or $year or $publisher or $number are set. So it can basically be any combination. Items belonging to the users in the $userIDS array and filtered by letter, year, publisher, number and maybe in the future other filters. Or it can be year and publisher, or publisher, number and year ... and so on.

2 Answers 2

1

Since you are trying to "Combination of the constraints or conditions", I can suggest you the below pattern that i've used for shops:

  1. define a class
  2. make rules
  3. get query
  4. optimize for cache or other things

     class condition {
    
     public function add($query) {
         $this->query[] = $query;
         return $this;
     }
    
     public function get() {
         return " SELECT * FROM TABLE WHERE " . implode(" and " ,$this->query);
    
     }
    
    
     $con = new condition();
      $query =   $con->add("price > 100 and price < 200")->add("year > 1980")->get();
    
Sign up to request clarification or add additional context in comments.

4 Comments

so the way I have it setup is wrong ? or will it be slow?
It's more about how you will maintain it in future, sometimes simple solution are quick but hard to manage in the portion of time, probably close future!
I understand. I ll try your way. But it's still a little confusing. I have to write it out to see everything. Thank you.
Understanding the concept of chaining is a major effect on this pattern development.
0

I use something similar to this at work, but obviously not as thrown together:

$search = [
    'fields' => [
        'name',
        'phone',
        'email'
    ],
    'filter' => [
        'name LIKE' => 'Joe'
    ]
];

function search(array $search) {
    $allowedFields = [
        'name',
        'phone',
        'email',
        'address',
    ];

    $allowedFilters = [
        'name',
        'phone',
        'email',
    ];

    $allowedActions = [
        '=',
        'LIKE',
        '<',
        '<=',
        '>',
        '>=',
    ];

    $fields = array();
    if(isset($search['fields'])) {
        foreach($search['fields'] as $field) {
            if(in_array($field, $allowedFields)) $fields[] = $field;
        }
    } else {
        $fields = $allowedFields;
    }

    $filters = array();
    if(isset($search['filters'])) {
        foreach($search['filters'] as $filter => $val) {
            $filter_split = explode(' ', $filter);
            $field = $filter;
            $action = '=';
            if(count($filter_split) > 1) {
                $field = $filter_split[0];
                $action = $filter_split[1];
            }

            if(!in_array($action, $allowedActions)) continue;

            if(in_array($field, $allowedFilters)) $filters[] = "{$field} {$action} {$val}";
        }
    }

    $fields_str = implode(',', $fields);
    $query = "SELECT {$fields_str} FROM users "

    if(count($filters) > 0) {
        $first = true;
        foreach($filters as $filter) {
            $action = 'AND';
            if($first) {
                $action = 'WHERE';
                $first = false;
            }
            $query .= "{$action} {$filter} ";
        }
    }

    mysqli_query($db, $query);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.