I am new in using DataTable ServerSide Processing. I am confuse to call a PHP function inside the array of Columns.
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
$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. I am just wondering, where i have to call this function. This is a function that i have written inside 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. Please help. Thanks in advance
ssp.class.php
doesn't support a JOIN
. But we have a workaround for this:
Use sub-query in your $table
definition and replace dist_code
with disname
in $columns
as shown below:
$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.
If you don't want to edit ssp.class.php
file, you can create a view in your database:
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:
$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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.