我使用了datatable jquery plugin.i想要将值绑定到我的数据库中不可用的列。例如,我有列名'Leave Balance',它是由query.i计算的,还有两个列,它是通过查询计算的.so如何在服务器端定义“Leave Balance”列?我怎样才能找回它。请帮帮我。
我在服务器端尝试过以下代码。在我的代码'Date'中,LeaveBalance','NumberOfDays'列由query.so如何将其与datatable绑定。
<?php
//$path=realpath ('wp-content/plugins/Employee_Time_Tracker/connection.php');
//include $_SERVER['DOCUMENT_ROOT'].'\cosmos\wp-content\plugins\Employee_Time_Tracker\connection.php';
/*get database values*/
$databasename=$_POST['databasename'];
$databaseuser=$_POST['databaseuser'];
$databasehost=$_POST['databasehost'];
$databasepassword=$_POST['databasepassword'];
/* Database connection start */
$servername = $databasehost;
$username = $databaseuser;
$password = $databasepassword;
$dbname = $databasename;
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
/* Database connection end */
global $wpdb;
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
//echo $requestData;
$columns = array(
// datatable column index => database column name
0=>'Date',
1=>'display_name',
2=> 'LeaveType',
3 => 'LeaveBalance',
4 => 'NumberOfDays',
5 => 'Status',
6 => 'ID'
);
// getting total number records without any search
$sql = "SELECT leaveassign.FromDate,user.display_name,leaveassign.LeaveType,leaveassign.Status,(leaveentitle.Days-datediff(leaveassign.ToDate, leaveassign.FromDate) ) as LeaveBalance,datediff(leaveassign.ToDate, leaveassign.FromDate) as NumberOfDays,leaveassign.leaveassign.ID ";
$sql.="FROM `wp_ag_assign_leave` as leaveassign INNER join wp_ag_leave_entitlement as leaveentitle INNER JOIN wp_users as user ON leaveassign.UserId=user.ID";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.//
$sql = "SELECT leaveassign.FromDate,user.display_name,leaveassign.LeaveType,leaveassign.Status,(leaveentitle.Days-datediff(leaveassign.ToDate, leaveassign.FromDate) ) as LeaveBalance,datediff(leaveassign.ToDate, leaveassign.FromDate) as NumberOfDays,leaveassign.leaveassign.ID ";
$sql.="FROM `wp_ag_assign_leave` as leaveassign INNER join wp_ag_leave_entitlement as leaveentitle INNER JOIN wp_users as user ON leaveassign.UserId=user.ID";
//if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
// $sql.=" where ( user.display_name LIKE '".$requestData['search']['value']."%' ";
// $sql.=" OR leaveassign.LeaveType LIKE '".$requestData['search']['value']."%' ";
//
// $sql.=" OR leaveassign.Status LIKE '".$requestData['search']['value']."%' ";
// $sql.=" OR LeaveBalance LIKE '".$requestData['search']['value']."%' ";
//
//
// $sql.=" OR NumberOfDays LIKE '".$requestData['search']['value']."%' )";
//}
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData[] = $row["FromDate"];
$nestedData[] = $row["display_name"];
$nestedData[] = $row["LeaveType"];
$nestedData[] = $row["LeaveBalance"];
$nestedData[] = $row["NumberOfDays"];
$nestedData[] = $row["Status"];
$nestedData[] = $row["ID"];
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ),
"data" => $data,
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
);
echo json_encode($json_data);
?>