How to use or-operator or grouping

How to use or-operator or grouping

CapamaniaCapamania Posts: 233Questions: 81Answers: 5
edited September 2016 in Editor

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.

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    You need to use a closure as described in the manual here.

    Basically just put your $_POST logic checks inside the closure.

    Allan

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    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 with where and use or_where inside the closure.

    Allan

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5

    Thanks Allan. I changed it accordingly ... but now it's not doing any filtering though:

    ->where( function ( $docs ) {
            if ( $_POST["selectMonthly"] ) {
                $docs->or_where( 'mr', $_POST["selectMonthly"], '>=' );
                    }
            if ( $_POST["selectQuarterly"] ) {
                $docs->or_where( 'qr', $_POST["selectQuarterly"], '>=' );
                    }
    } )
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    That looks correct to me code-wise. Are those statements actually being executed?

    Allan

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5
    edited September 2016

    Not sure how to check this ... but the table is refreshing, but no filtering.

    What does work though is the following:

    ->where( function ( $docs ) {
            if ( $_POST["selectMonthly"] ) {
                $docs->where( 'mr', $_POST["selectMonthly"], '>=' );
                    }
            if ( $_POST["selectQuarterly"] ) {
                $docs->or_where( 'qr', $_POST["selectQuarterly"], '>=' );
                    }
    } )
    

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

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    As a workaround while I look into this, you could do something like:

    ->where( function ( $docs ) {
            if ( $_POST["selectMonthly"] ) {
                $docs->where( 'mr', $_POST["selectMonthly"], '>=' );
            }
            if ( $_POST["selectQuarterly"] ) {
                if ( $_POST["selectMonthly"] ) {
                    $docs->or_where( 'qr', $_POST["selectQuarterly"], '>=' );
                }
                else {
                    $docs->where( 'qr', $_POST["selectQuarterly"], '>=' );
                }
           }
    } )
    

    Ugly, but it should work.

    Allan

This discussion has been closed.