如何使用ssp.class.php DataTables使用'WHERE'子句

STi*_*KED 15 php mysql sql jquery jquery-datatables

好的,所以我试图使用jQuery DataTable(DataTables.net)显示来自我的数据库的信息.我可以让它工作得很好,显示整个表'笔记',但我想只显示未读取的笔记.所以我需要以某种方式包含一个WHERE子句,但我不清楚最好的方法来解决这个问题.

这是我目前显示整个表格的方式:

// DB table to use
$table = 'Notes';

// Table's primary key
$primaryKey = 'CID';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'CID', 'dt' => 0 ),

array(
    'db'        => 'CID',
    'dt'        => 0,
    'formatter' => function( $d, $row ) {
        return '<a href="profile.php?search='.$d.'" target="_Blank">'.$d."</a>";
    }
),

array( 'db' => 'Title', 'dt' => 1 ),
array( 'db' => 'Name',  'dt' => 2 ),
array(
    'db'        => 'Date',
    'dt'        => 3,
    'formatter' => function( $d, $row ) {
        return date( 'jS M y', strtotime($d));
        }
    )
);

// SQL server connection information
$sql_details = array(
'user' => '*DB_USER*',
'pass' => '*Password*',
'db'   => '*DatabaseName*',
'host' => 'localhost'
);
require( 'ssp.class.php' );

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
Run Code Online (Sandbox Code Playgroud)

我需要相当于 SELECT * FROM Notes WHERE Status ='Unread'

Pou*_*abi 31

您必须更改DataTables默认功能才能执行此操作!

使用这个ssp.class.php自定义类

链接

如何使用 ?

像这个例子一样使用:

require( 'ssp.class.php' );
$where = "Status ='Unread'";
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns,$where )
);
Run Code Online (Sandbox Code Playgroud)

如果你设置参数$ where,这个类添加选择语句的位置!

更新:

2015年的DataTables增加了复杂的方法

这个新方法可以在查询中设置where子句!

  • 你的代码中有一个错误,它应该是:SSP :: complex($ _GET,$ sql_details,$ table,$ primaryKey,$ columns,$ where)复杂的方法是添加了对where子句的支持的方法,使用SSP:simple将忽略您刚刚添加的查询.我已经编辑了你的答案.查看文档:https://github.com/DataTables/DataTablesSrc/blob/master/examples/server_side/scripts/ssp.class.php#L276 (4认同)

Bre*_*hse 8

嗯..你不能没有编辑或扩展SSP.这是非常糟糕的风格,有很多复制的代码,但SSP不允许更好的自定义...

class SSPCustom extends SSP
{
    /**
     *  @param  array $request Data sent to server by DataTables
     *  @param  array $sql_details SQL connection details - see sql_connect()
     *  @param  string $table SQL table to query
     *  @param  string $primaryKey Primary key of the table
     *  @param  array $columns Column information array
     *  @param  string $whereCustom Custom (additional) WHERE clause
     *  @return array          Server-side processing response array
     */
    static function simpleCustom ( $request, $sql_details, $table, $primaryKey, $columns, $whereCustom = '' )
    {
        $bindings = array();
        $db = self::sql_connect( $sql_details );

        // Build the SQL query string from the request
        $limit = self::limit( $request, $columns );
        $order = self::order( $request, $columns );
        $where = self::filter( $request, $columns, $bindings );

        if ($whereCustom) {
            if ($where) {
                $where .= ' AND ' . $whereCustom;
            } else {
                $where .= 'WHERE ' . $whereCustom;
            }
        }

        // Main query to actually get the data
        $data = self::sql_exec( $db, $bindings,
            "SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", self::pluck($columns, 'db'))."`
             FROM `$table`
             $where
             $order
             $limit"
        );

        // Data set length after filtering
        $resFilterLength = self::sql_exec( $db,
            "SELECT FOUND_ROWS()"
        );
        $recordsFiltered = $resFilterLength[0][0];

        // Total data set length
        $resTotalLength = self::sql_exec( $db,
            "SELECT COUNT(`{$primaryKey}`)
             FROM   `$table`
             WHERE  " . $whereCustom
        );
        $recordsTotal = $resTotalLength[0][0];


        /*
         * Output
         */
        return array(
            "draw"            => intval( $request['draw'] ),
            "recordsTotal"    => intval( $recordsTotal ),
            "recordsFiltered" => intval( $recordsFiltered ),
            "data"            => self::data_output( $columns, $data )
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

称之为:

echo json_encode(
    SSPCustom::simpleCustom( $_GET, $sql_details, $table, $primaryKey, $columns, "Status ='Unread'" )
);
Run Code Online (Sandbox Code Playgroud)

未经测试