当where子句具有指定值时,单个过滤器不起作用

时间:2017-05-23 04:03:57

标签: php datatables filtering where-clause datatables-1.10

我设置我的数据表只显示以OQC开头的数据。

require('ssp.class.join.php' );

    $joinQuery = "FROM `monitor` AS `a` JOIN `sym_category` AS `b` JOIN `model_cat` AS `c`";
    $joinQuery.= "ON (`b`.`id_sym` = `a`.`id_sym`) AND (`c`.`id_mod` = `a`.`id_mod`)";
    $joinQuery.= " WHERE `a`.`id_car` LIKE 'OQC%'";

    echo json_encode(
       SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, $joinQuery )
    );

ssp.class.join.php:

static function filter ( $request, $columns, &$bindings, $isJoin = false )
    {
        $globalSearch = array();
        $columnSearch = array();
        $dtColumns = SSP::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 = SSP::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                    $globalSearch[] = ($isJoin) ? $column['db']." LIKE ".$binding : "`".$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 = SSP::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                $columnSearch[] = ($isJoin) ? $column['db']." LIKE ".$binding : "`".$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;
    }

显示错误:syntax error near 'WHERE a.id_car.....'。 我应该改变哪一部分,以使两者都有效?

1 个答案:

答案 0 :(得分:0)

你可能应该查看连接的确切查询,因为它中存在语法错误。试着看看ResultSet到底是什么(或者更好,整个查询,因为那有什么错误)

猜测,可能需要在最后一行的开头有一个空格

$where