如何让数据表服务器端处理脚本与自定义查询一起工作?我需要从多个表中选择列并让数据表呈现它们。
Datatables.net 使用 PHP 的服务器端处理 (SSP) 总结如下:https://datatables.net/examples/server_side/simple.html
我找到了这个 SO question ,但原始海报从未提供他的解决方案。我没有足够的声誉要求他提供更多细节。
这是我未使用 Datatable 的 SSP 的原始 SQL
SELECT tbl_houses.style, tbl_houses.roomCount, tbl_residents.firstName, tbl_residents.lastName
FROM tbl_houses, tbl_residents
WHERE tbl_houses.houseID = tbl_residents.residentID
/*
* # Equivalent query using JOIN suggested by @KumarRakesh
* # Note: JOIN ... ON is a synonym for INNER JOIN ... ON
* # Using JOIN conforms to syntax spec'd by ANSI-92 https://stackoverflow.com/a/894855/946957
*
* SELECT tbl_houses.style, tbl_houses.roomCount, tbl_residents.firstName, tbl_residents.lastName
* FROM tbl_houses
* JOIN tbl_residents ON tbl_houses.houseID = tbl_residents.residentID
*/
如何使用 SSP 让 Datatables 运行上述查询?
出现server_processing.php仅接受 1 个表且不接受自定义过滤(即 WHERE
子句)。
// DB table to use
$table = 'datatables_demo';
// Table's primary key
$primaryKey = 'id';
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
然而,ssp.class.php 是否 支持使用WHERE
进行过滤。我想我需要修改 ssp.class.php
以强制加入我的 WHERE
子句
更新
找到解决方案。有空的时候会发帖。
最佳答案
解决方案
Class ssp.class.php
不支持连接和子查询,但有一个解决方法。诀窍是使用子查询,如下面的 $table
定义所示。将子查询中的 table
替换为您的实际表名。
$table = <<<EOT
(
SELECT
a.id,
a.name,
a.father_id,
b.name AS father_name
FROM table a
LEFT JOIN table b ON a.father_id = b.id
) temp
EOT;
$primaryKey = 'id';
$columns = array(
array( 'db' => 'id', 'dt' => 0 ),
array( 'db' => 'name', 'dt' => 1 ),
array( 'db' => 'father_id', 'dt' => 2 ),
array( 'db' => 'father_name', 'dt' => 3 )
);
$sql_details = array(
'user' => '',
'pass' => '',
'db' => '',
'host' => ''
);
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
您还需要编辑 ssp.class.php
并将 FROM `$table`
的所有实例替换为 FROM $table
以删除反引号。
确保所有列名都是唯一的,否则使用 AS
分配别名。
注意事项
还有github.com/emran/ssp包含增强的 ssp.class.php
支持 JOIN 的存储库。
链接
参见 jQuery DataTables: Using WHERE, JOIN and GROUP BY with ssp.class.php获取更多信息。
关于php - 数据表 + PHP : Server-Side Processing on Multiple Tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39183330/