How to use 'WHERE' clause using ssp.class.php DataTables

前端 未结 4 1608
独厮守ぢ
独厮守ぢ 2021-02-01 07:36

Okay so i\'m trying to display information from my database using jQuery DataTable (DataTables.net). I can get it to work fine displaying the entire table \'notes\' but I would

4条回答
  •  感动是毒
    2021-02-01 08:00

    I was also able to solve this problem but inserting some code into the ssp.class.php filter function. Below is the listing for the function with an example custom where clause inserted. The "simple" function from that class will work without any further jury-rigging. The advantage is that it will play nice with the text-search feature of the datatable.

    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);
        }
    
            //------------------------------------------------------------
            //############################################################
            //EXAMPLE ADDITIONAL WHERE CONDITIONS HERE. THIS IS EQUIVALENT
            //TO "WHERE id = 1"
            $where = ($where === '') ? 
                "id = ".self::bind( $bindings, 1, PDO::PARAM_INT) :
                $where ." AND "."id = ".self::bind( $bindings, 1, PDO::PARAM_INT);
            //############################################################
            //############################################################
            //------------------------------------------------------------
    
    
        if ( $where !== '' ) {
            $where = 'WHERE '.$where;
        }
    
        return $where;
    }
    

提交回复
热议问题