Hi,
I'm trying to use datatables with couchcms, whereby I use serverside option. When I use server_processing file as a non-couch template, it works very well. However, when I make it a couch template, it does not work. Here is the code:
Front end script:
$(document).ready(function() {
var oTable = $('#datatable1').DataTable(
{
"processing": true,
"serverSide": true,
"responsive": true,
"ajax": {
"url": "server_processing.php"
}
});
And server_processing.php file is:
<?php require_once( 'couch/cms.php' ); ?>
<?php require_once( 'config.php' ); ?>
<?php require_once( 'ssp.customized.class.php' ); ?>
<cms:template title='Server Side' />
<cms:php>
global $db_username;
global $db_password;
global $db_name;
global $db_host;
// DB table to use
$table = 'user';
// Table's primary key
$primaryKey = 'id';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => '`u`.`first_name`', 'dt' => 1, 'field' => 'first_name' ),
array( 'db' => '`u`.`last_name`', 'dt' => 2, 'field' => 'last_name' ),
array( 'db' => '`u`.`start_date`', 'dt' => 3, 'field' => 'start_date', 'formatter' => function( $d, $row ) {
return date( 'Y-m-d', strtotime($d));
}),
array( 'db' => '`ud`.`email`', 'dt' => 4, 'field' => 'email' ),
array('db' => '`u`.`salary`', 'dt' => 5, 'field' => 'salary', 'formatter' => function( $d, $row ) {
return '$'.number_format($d);
})
);
// SQL server connection information
$sql_details = array(
'user' => $db_username,
'pass' => $db_password,
'db' => $db_name,
'host' => $db_host
);
$joinQuery = "FROM `user` AS `u` JOIN `user_details` AS `ud` ON (`ud`.`user_id` = `u`.`id`)";
$extraWhere = "`u`.`office` = 'Tokyo'";
$result=SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, $joinQuery, $extraWhere, $groupBy );
$start=$_REQUEST['start']+1;
$idx=0;
foreach($result['data'] as &$res){
$res[0]=(string)$start;
$start++;
$idx++;
}
echo json_encode($result);
</cms:php>
<?php COUCH::invoke(); ?>
Please help me, I'm stuck.
Thanks
I'm trying to use datatables with couchcms, whereby I use serverside option. When I use server_processing file as a non-couch template, it works very well. However, when I make it a couch template, it does not work. Here is the code:
Front end script:
$(document).ready(function() {
var oTable = $('#datatable1').DataTable(
{
"processing": true,
"serverSide": true,
"responsive": true,
"ajax": {
"url": "server_processing.php"
}
});
And server_processing.php file is:
<?php require_once( 'couch/cms.php' ); ?>
<?php require_once( 'config.php' ); ?>
<?php require_once( 'ssp.customized.class.php' ); ?>
<cms:template title='Server Side' />
<cms:php>
global $db_username;
global $db_password;
global $db_name;
global $db_host;
// DB table to use
$table = 'user';
// Table's primary key
$primaryKey = 'id';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => '`u`.`first_name`', 'dt' => 1, 'field' => 'first_name' ),
array( 'db' => '`u`.`last_name`', 'dt' => 2, 'field' => 'last_name' ),
array( 'db' => '`u`.`start_date`', 'dt' => 3, 'field' => 'start_date', 'formatter' => function( $d, $row ) {
return date( 'Y-m-d', strtotime($d));
}),
array( 'db' => '`ud`.`email`', 'dt' => 4, 'field' => 'email' ),
array('db' => '`u`.`salary`', 'dt' => 5, 'field' => 'salary', 'formatter' => function( $d, $row ) {
return '$'.number_format($d);
})
);
// SQL server connection information
$sql_details = array(
'user' => $db_username,
'pass' => $db_password,
'db' => $db_name,
'host' => $db_host
);
$joinQuery = "FROM `user` AS `u` JOIN `user_details` AS `ud` ON (`ud`.`user_id` = `u`.`id`)";
$extraWhere = "`u`.`office` = 'Tokyo'";
$result=SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, $joinQuery, $extraWhere, $groupBy );
$start=$_REQUEST['start']+1;
$idx=0;
foreach($result['data'] as &$res){
$res[0]=(string)$start;
$start++;
$idx++;
}
echo json_encode($result);
</cms:php>
<?php COUCH::invoke(); ?>
Please help me, I'm stuck.
Thanks