简体   繁体   English

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

[英]Calling a function in DataTable Server Side Processing

I am new in using DataTable ServerSide Processing.我是使用 DataTable ServerSide Processing 的新手。 I am confuse to call a PHP function inside the array of Columns.我很困惑在列数组中调用 PHP 函数。

Here is a Front-end code.这是前端代码。

<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

<?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 )
);

Both files are producing perfect results.这两个文件都产生了完美的结果。 Output输出输出

I just want to show District name instead of District code.我只想显示地区名称而不是地区代码。 I have a function written in functions.php and that function is able to fetch the district name from database.我有一个用functions.php 编写的函数,该函数能够从数据库中获取地区名称。 I am just wondering, where i have to call this function.我只是想知道,我必须在哪里调用这个函数。 This is a function that i have written inside function.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'];

        }
    }
}

Actually i don't know, how to call this function inside the $column array.其实我不知道,如何在 $column 数组中调用这个函数。 Please help.请帮忙。 Thanks in advance提前致谢

ssp.class.php doesn't support a JOIN . ssp.class.php不支持JOIN But we have a workaround for this:但是我们有一个解决方法:

Solution 1 (Use sub-query):解决方案1(使用子查询):

Use sub-query in your $table definition and replace dist_code with disname in $columns as shown below:$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 )
);

Then, you need to replace all instances of `$table` with $table to remove backticks in ssp.class.php file.然后,您需要将$table所有实例替换`$table` $table以删除ssp.class.php文件中的反引号。

Solution 2 (Create a view):解决方案 2(创建视图):

If you don't want to edit ssp.class.php file, you can create a view in your database:如果你不想编辑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;

Then, use requests_view as your $table in getData.php file:然后,在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 )
);

You may also consider to use third party PHP libraries like Customized SSP Class For Datatables Library or Datatables library for PHP which support JOIN s.您还可以考虑使用第三方 PHP 库,例如支持JOIN用于数据表库的自定义 SSP 类库用于 PHP 的数据表库

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

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