Is it possible? (Server Side Processing)

Is it possible? (Server Side Processing)

samuel89757samuel89757 Posts: 7Questions: 3Answers: 0

Hi, it is possible to do a if else on search/filtering in the server side (ssp.class).

For example:
If I want to do a exact search I need to put ("test") to search else I will search everything with global search.

Hope you can give me some advise on how should I do it.

static function filter ( $request, $columns, &$bindings )
{
    $globalSearch = array();
    $columnSearch = array();
    $dtColumns = self::pluck( $columns, 'dt' );

    if ( isset($request['search']) && $request['search']['value'] != '' ) {
        $str = $request['search']['value'];

        for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
            $requestColumn = $request['columns'][$i];
            $columnIdx = array_search( $requestColumn['data'], $dtColumns );
            $column = $columns[ $columnIdx ];

            if ( $requestColumn['searchable'] == 'true' ) {
                $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                $globalSearch[] = "`".$column['db']."` LIKE ".$binding;
            }
        }
    }

    // Individual column filtering
    for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
        $requestColumn = $request['columns'][$i];
        $columnIdx = array_search( $requestColumn['data'], $dtColumns );
        $column = $columns[ $columnIdx ];

        $str = $requestColumn['search']['value'];

        if ( $requestColumn['searchable'] == 'true' &&
         $str != '' ) {
            $binding = self::bind( $bindings,'%'.$str.'%', PDO::PARAM_STR );
            $columnSearch[] = "`".$column['db']."` LIKE ".$binding;
        }
    }

    // Combine the filters into a single string
    $where = '';

    if ( count( $globalSearch ) ) {
        $where = '('.implode(' OR ', $globalSearch).')';
    }

    if ( count( $columnSearch ) ) {
        $where = $where === '' ?
            implode(' AND ', $columnSearch) :
            $where .' AND '. implode(' AND ', $columnSearch);
    }

    if ( $where !== '' ) {
        $where = 'WHERE '.$where;
    }

    return $where;
}

Answers

  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    edited November 2014

    Sure, but you'll need to modify the code. To do an exact search just do a standard SQL = in the condition rather than using the LIKE parameter in my demo code you've pasted above.

    Allan

  • samuel89757samuel89757 Posts: 7Questions: 3Answers: 0

    can you show an example? The = I know how to apply but I want to know how to apply the if else.

  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin

    Lines 32 and 33 above would just be wrapped in an if / else. What the logic is to decide if if condition I'm not sure though. Are you sending extra data, or using the column name or something else?

    Happy to provide an example showing how it might be done - DataTables support can be purchased here.

    Allan

This discussion has been closed.