Enhanced filters in Pagination - how?

Asked by Brian Rippon on 2011-02-24

The Pagination plugin is great, but I'd like to be able to provide enhanced search filter functionality beyond "a LIKE '%x%' OR b LIKE '%x'...". For example, 'Power > 1000', or 'Price > 2000 AND Price < 4000'.

Is there a way that I can hook extra form fields/filters into Pagination, or is it a case of building a new plugin based on it?

Question information

Language:
English Edit question
Status:
Answered
For:
PHPDevShell Edit question
Assignee:
No assignee Edit question
Last query:
2011-02-24
Last reply:
2011-02-25
TitanKing (titan-phpdevshell) said : #1

You can modify the query just as you wish... look at the database object for more information, but what it comes down to, is simply pass variable to your sql like this:

class SomeQuery extends PHPDS_query
{
 protected $sql = "
  SELECT
   some_column, some_column2, Price
  FROM
   _db_some_table
  WHERE
   Price > %u AND Price < %u
 ";

 public function invoke($parameters)
 {
  // Do all the invoke for pagination here...

  // Now push replacements for %u
  $price1 = 2000;
  $price2 = 4000;

  $query = $pagination->query($this->sql, $price1, $price2);

 }
}

Now just add dynamic data as you wish...

TitanKing (titan-phpdevshell) said : #2

Then I see you ask about forms...

Inside your view create the form you wish for, the submit button should be called 'save' and then the search field should be called 'search_field', then the rest of the filter form can be whatever you choose. Then just pass the rest of the forms filters as parameters to the query.

Also note that, in above example we already used WHERE, so you might want to explicitly tell the query the rest of the query should be :
$pagination->condition = 'AND'; // This should be automatically detected though.

Brian Rippon (brian-rippon) said : #3

Sorry, should have said I was happy about modifying $sql to contain my WHERE clauses, although I hadn't thought to add the parameters to $pagination->query(). And it was how to modify the form that troubled me.

I'm still thinking about your reply, but I assume I have to code my own form in the view, instead of $pagination->searchForm()? I'm not yet familiar enough with the code to understand how this function works. I'm also trying to figure out the significance of the name 'save' for the submit button...

I'm getting there - many thanks.

TitanKing (titan-phpdevshell) said : #4

Hi Brian,

Yes, you create your own form in the view instead of using the standard auto drawn one. The reason why I say you need to ha a $post['save'] and $post['search_field'] available is because the standard filter plugin looks for this and uses this to do the search query if search field is used. This is so that the standard functionality of your form still works.

Can you help with this problem?

Provide an answer of your own, or ask Brian Rippon for more information if necessary.

To post a message you must log in.