SSP Server-side Word by Word, applied to columns

SSP Server-side Word by Word, applied to columns

east1999east1999 Posts: 31Questions: 11Answers: 0

Hi,

I would like to know if anyone has a solution for applying smart search to columns, using server-side processing.

I know it's a regular question in this forum, but there is a working solution in this thread, which I'm copying below. It works like a charm for my global search, but I'm trying to use within column search as well, and simply copy pasting the code for individual columns filtering is not working.

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

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

         for ( $x=0, $l=count($str) ; $x<$l ; $x++ ) {
             if ( $str[$x] != '' ) {
                 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 = SSP::bind( $bindings, '%'.$str[$x].'%', PDO::PARAM_STR );
                         $globalSearch[$x][] = "`".$column['db']."` LIKE ".$binding;
                     }
                 }
             }
         }
     }

     // Individual column filtering
     if ( isset( $request['columns'] ) ) {
         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 ) ) {
         foreach ($globalSearch as $globalSearchar){
             $where .= ' AND ('.implode(' OR ', $globalSearchar).')';
         }
     }

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

         if ( $where !== '' ) {
        //remove the first AND
            if ( count( $globalSearch ) ) {
        $where  = preg_replace('/AND/', '', $where, 1);
            }
      $where = 'WHERE '.$where;
         }

     return $where;
     }

Answers

  • east1999east1999 Posts: 31Questions: 11Answers: 0
    edited February 2019

    Just to answer my own question, there's a thread at Stack Overflow that addresses this. Here's how it works for Individual column filtering:

             if ( isset( $request['columns'] ) ) {
                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'];
                    $strArray = explode(' ',$str);
                        foreach ($strArray as $str) {
                        $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                        $columnSearch[] = "`".$column['db']."` LIKE ".$binding;
                    }
                }
            }
    
This discussion has been closed.