在 DataTable 服务器端处理中调用函数

我是使用 DataTable 服务器端处理的新手。我很困惑在 Columns 数组中调用 PHP 函数。


这是前端代码。


<table id="memListTable" class="display" style="width:100%">

    <thead>

        <tr>

            <th>Request Date</th>

            <th>District Name</th>

            <th>Request Type</th>

        </tr>

    </thead>

    <tfoot>

        <tr>

            <th>Request Date</th>

            <th>District</th>

            <th>Request Type</th>

        </tr>

    </tfoot>

</table> 

<script>

$(document).ready(function(){

    $('#memListTable').DataTable({

        "processing": true,

        "serverSide": true,

        "aaSorting": [[0,'desc']],

        "ajax": "getData.php"

    });

});

</script>

获取数据.php


<?php

$dbDetails = array(

'host' => '****',

'user' => '****',

'pass' => '****',

'db'   => '****'

);


$table = 'requestss';


$primaryKey = 'id';


$columns = array(

array( 'db' => 'time_stamp',  'dt' => 0 ),

array( 'db' => 'dist_code',  'dt' => 1),

array( 'db' => 'req_type',  'dt' => 2 )

);


// Include SQL query processing class

require( 'ssp.class.php' );


// Output data as json format

echo json_encode(

SSP::simple( $_GET, $dbDetails, $table, $primaryKey, $columns )

);

这两个文件都产生了完美的结果。输出 


慕娘9325324
浏览 87回答 1
1回答

人到中年有点甜

ssp.class.php不支持JOIN. 但是我们有一个解决方法:解决方案1(使用子查询):在定义中使用子查询$table并替换dist_code为disnamein $columns,如下所示:$dbDetails = [&nbsp; &nbsp; 'host' => '****',&nbsp; &nbsp; 'user' => '****',&nbsp; &nbsp; 'pass' => '****',&nbsp; &nbsp; 'db'&nbsp; &nbsp;=> '****'];$table = '(SELECT r.*, d.disname FROM requestss r INNER JOIN districts d ON r.dist_code = d.discode) tbl';$primaryKey = 'id';$columns = [&nbsp; &nbsp; [ 'db' => 'time_stamp',&nbsp; 'dt' => 0 ],&nbsp; &nbsp; [ 'db' => 'disname',&nbsp; 'dt' => 1 ],&nbsp; &nbsp; [ 'db' => 'req_type',&nbsp; 'dt' => 2 ]];// Include SQL query processing classrequire( 'ssp.class.php' );// Output data as json formatecho json_encode(&nbsp; &nbsp; SSP::simple( $_GET, $dbDetails, $table, $primaryKey, $columns ));然后,您需要替换 with 的所有实例`$table`以$table删除ssp.class.php文件中的反引号。解决方案 2(创建视图):如果您不想编辑ssp.class.php文件,可以在数据库中创建一个视图:CREATE&nbsp; &nbsp; VIEW requests_view&nbsp; &nbsp; AS SELECT r.*, d.disname FROM requestss r INNER JOIN districts d ON r.dist_code = d.discode;然后,requests_view用作您的$tableingetData.php文件:$dbDetails = [&nbsp; &nbsp; 'host' => '****',&nbsp; &nbsp; 'user' => '****',&nbsp; &nbsp; 'pass' => '****',&nbsp; &nbsp; 'db'&nbsp; &nbsp;=> '****'];$table = 'requests_view';$primaryKey = 'id';$columns = [&nbsp; &nbsp; [ 'db' => 'time_stamp',&nbsp; 'dt' => 0 ],&nbsp; &nbsp; [ 'db' => 'disname',&nbsp; 'dt' => 1 ],&nbsp; &nbsp; [ 'db' => 'req_type',&nbsp; 'dt' => 2 ]];// Include SQL query processing classrequire( 'ssp.class.php' );// Output data as json formatecho json_encode(&nbsp; &nbsp; SSP::simple( $_GET, $dbDetails, $table, $primaryKey, $columns ));您还可以考虑使用第三方 PHP 库,例如自定义 SSP 类用于数据表库或用于 PHP的支持JOINs 的数据表库。
打开App,查看更多内容
随时随地看视频慕课网APP