How to use or-operator or grouping
How to use or-operator or grouping
I have two filters which for work:
->where( function ($monthly) {
if ( $_POST["selectMonthly"] ) {
$monthly->where( 'mr', $_POST["selectMonthly"], '>=' );
}
} )
->where( function ($quarterly) {
if ( $_POST["selectQuarterly"] ) {
$quarterly->where( 'qr', $_POST["selectQuarterly"], '>=' );
}
} )
But if I use both filter ... I get no results in the table. What I would like is getting both results though. I guess I need to somehow use https://editor.datatables.net/manual/php/conditions#Or-operators-and-grouping. I tried the following ... but it returns me an php error:
->or_where( function ( $docs ) {
if ( $_POST["selectMonthly"] ) {
$docs->where( 'mr', $_POST["selectMonthly"], '>=' );
}
if ( $_POST["selectQuarterly"] ) {
$docs->where( 'pr', $_POST["selectQuarterly"], '>=' );
}
} )
The table looks like this:
id|mr|qr|
1|1||
2||1|
The option value of the filter is 1.
This discussion has been closed.
Answers
You need to use a closure as described in the manual here.
Basically just put your
$_POST
logic checks inside the closure.Allan
Hmm - sorry, I just spotted that you linked to that section.
It looks like you are using an
or_where
method at the top level (which isn't valid - the documentation doesn't use that). Replace that withwhere
and useor_where
inside the closure.Allan
Thanks Allan. I changed it accordingly ... but now it's not doing any filtering though:
That looks correct to me code-wise. Are those statements actually being executed?
Allan
Not sure how to check this ... but the table is refreshing, but no filtering.
What does work though is the following:
... so if I filter for 'mr' first and by itself ... it works. And also if I then in addition filter for 'qr' ... which in combination shows the expected results.
Yet, if I filter for 'qr' first and by itself ... no filtering. If I filter then in addition for 'mr' ... the combined result is as expected.
So I guess or_where doesn't like to be alone ...
As a workaround while I look into this, you could do something like:
Ugly, but it should work.
Allan