在DataTable服务器端处理中调用函数 [英] Calling a function in DataTable Server Side Processing

查看:0
本文介绍了在DataTable服务器端处理中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用DataTable ServerSide处理的新手。在列数组中调用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>

getData.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 )
);

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

我只想显示地区名称,而不是地区代码。我有一个用unctions.php编写的函数,该函数能够从数据库中获取地区名称。我只是在想,我必须在哪里调用这个函数。这是我在函数中编写的函数。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定义中使用子查询,将dist_code替换为$columns中的disname,如下所示:

$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以删除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;

然后,使用requests_view作为getData.php文件中的$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 )
);

您还可以考虑使用支持JOINCustomized SSP Class For Datatables LibraryDatatables library for PHP等第三方PHP库。

这篇关于在DataTable服务器端处理中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆