亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 DataTable 服務器端處理中調用函數

在 DataTable 服務器端處理中調用函數

PHP
慕娘9325324 2022-07-22 10:04:59
我是使用 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 classrequire( 'ssp.class.php' );// Output data as json formatecho json_encode(SSP::simple( $_GET, $dbDetails, $table, $primaryKey, $columns ));這兩個文件都產生了完美的結果。輸出 
查看完整描述

1 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

ssp.class.php不支持JOIN. 但是我們有一個解決方法:


解決方案1(使用子查詢):

在定義中使用子查詢$table并替換dist_code為disnamein $columns,如下所示:


$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 )

);

然后,您需要替換 with 的所有實例`$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用作您的$tableingetData.php文件:


$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 庫,例如自定義 SSP 類用于數據表庫或用于 PHP的支持JOINs 的數據表庫。


查看完整回答
反對 回復 2022-07-22
  • 1 回答
  • 0 關注
  • 96 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號