数据表+ PHP:多个表上的服务器端处理

Chr*_*ian 7 php mysql server-side datatables

如何让Datatables 服务器端处理脚本与自定义查询一起使用?我需要从多个表中选择列并让Datatables渲染它们.

Datatables.net的PHP服务器端处理(SSP)总结如下:https://datatables.net/examples/server_side/simple.html

我发现了这个问题,但原始海报从未提供过他的解决方案.我没有足够的声誉要求他提供更多细节.

这是我的原始SQL,没有使用Datatable的SSP

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
*/
Run Code Online (Sandbox Code Playgroud)

如何使用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 )
);
Run Code Online (Sandbox Code Playgroud)

但是,ssp.class.php 确实支持使用过滤WHERE.我想我需要修改ssp.class.php以强制执行我的WHERE条款

UPDATE

找到了解决方案.我有空闲时间会发布.

Gyr*_*com 7

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 )
);
Run Code Online (Sandbox Code Playgroud)

您还需要编辑ssp.class.php和替换所有FROM `$table`with的实例FROM $table以删除反引号.

确保所有列名都是唯一的,否则用于AS指定别名.

笔记

还有github.com/emran/ssp存储库,其中包含增强的ssp.class.php支持JOIN.

链接

请参阅jQuery DataTables:使用WHERE,JOIN和GROUP BY与ssp.class.php获取更多信息.


Chr*_*ian 3

TL;DR:我最终使用了由 Emran Ul Hadi 实现的原始数据表的修改:ssp.class.phphttps : //github.com/emran/sspssp.php

他的修改接受 JOIN、WHERE、GROUP BY 和列别名。尽管该文件已经一年多没有更新,但它仍然适用于 DataTables 1.12.x。我对他的版本进行了一些修改,提高了其稳健性,并通过更清晰的示例改进了文档。

当我有更多时间时,我会在这里发布我的模组/更新。最终,我希望提出一个拉取请求,将我的更新放入他的存储库中。