繁体   English   中英

调用DataTable服务端处理中的函数

[英]Calling a function in DataTable Server Side Processing

我是使用 DataTable ServerSide Processing 的新手。 我很困惑在列数组中调用 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 )
);

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

我只想显示地区名称而不是地区代码。 我有一个用functions.php 编写的函数,该函数能够从数据库中获取地区名称。 我只是想知道,我必须在哪里调用这个函数。 这是我在function.php 中写的一个函数

function getDistrict($dist_code,$con)
{
    $sql = "SELECT disname FROM districts WHERE discode=$dist_code";
    $query = mysqli_query($con,$sql);
    if($query)
    {
        while($row = mysqli_fetch_array($query))
        {
            return $value = $row['disname'];

        }
    }
}

其实我不知道,如何在 $column 数组中调用这个函数。 请帮忙。 提前致谢

ssp.class.php不支持JOIN 但是我们有一个解决方法:

解决方案1(使用子查询):

$table定义中使用子查询并在$columns中用disname替换dist_code ,如下所示:

$dbDetails = [
    'host' => '****',
    'user' => '****',
    'pass' => '****',
    'db'   => '****'
];

$table = '(SELECT r.*, d.disname FROM requestss r INNER JOIN districts d ON r.dist_code = d.discode) tbl';

$primaryKey = 'id';

$columns = [
    [ 'db' => 'time_stamp',  'dt' => 0 ],
    [ 'db' => 'disname',  'dt' => 1 ],
    [ '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 )
);

然后,您需要将$table所有实例替换`$table` $table以删除ssp.class.php文件中的反引号。

解决方案 2(创建视图):

如果你不想编辑ssp.class.php文件,你可以在你的数据库中创建一个视图:

CREATE
    VIEW requests_view
    AS SELECT r.*, d.disname FROM requestss r INNER JOIN districts d ON r.dist_code = d.discode;

然后,在getData.php文件中使用requests_view作为$table

$dbDetails = [
    'host' => '****',
    'user' => '****',
    'pass' => '****',
    'db'   => '****'
];

$table = 'requests_view';

$primaryKey = 'id';

$columns = [
    [ 'db' => 'time_stamp',  'dt' => 0 ],
    [ 'db' => 'disname',  'dt' => 1 ],
    [ '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 )
);

您还可以考虑使用第三方 PHP 库,例如支持JOIN用于数据表库的自定义 SSP 类库用于 PHP 的数据表库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM