好吧,我不知道如何使用 jquery 数据表服务器端加载 php 文件。我使用 sql server 并且它已连接。这是我的 html 代码
<table cellpadding="0" cellspacing="0" border="0" class="display" id="show_unit">
<thead>
<tr>
<th>Rendering engine</th>
<th>Browser</th>
<th>Platform(s)</th>
<th>Engine version</th>
<th>CSS grade</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th>Rendering engine</th>
<th>Browser</th>
<th>Platform(s)</th>
<th>Engine version</th>
<th>CSS grade</th>
</tr>
</tfoot>
</table>
<script>
$(document).ready(function() {
$("#show_unit").DataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/simda/server_side.php",
"iDeferLoading": 57
});
});
这是我的 server_side.php
<?php
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "id";
/* DB table to use */
$sTable = "Ref_Unit";
include "classes/koneksi.php";
$kon=new koneksi();
$conn=$kon->bukaKoneksi();
$params = array();
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
intval( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
$sWhere = "";
// if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
// {
// $sWhere = "WHERE (";
// for ( $i=0 ; $i<count($aColumns) ; $i++ )
// {
// $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
// }
// $sWhere = substr_replace( $sWhere, "", -3 );
// $sWhere .= ')';
// }
/* Individual column filtering */
// for ( $i=0 ; $i<count($aColumns) ; $i++ )
// {
// if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
// {
// if ( $sWhere == "" )
// {
// $sWhere = "WHERE ";
// }
// else
// {
// $sWhere .= " AND ";
// }
// $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
// }
// }
$options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = sqlsrv_query($conn, $sQuery, $params, $options);
/* Data set length after filtering */
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = sqlsrv_query($conn, $sQuery, $params, $options);
$aResultFilterTotal = sqlsrv_fetch_array($rResultFilterTotal, SQLSRV_FETCH_ASSOC);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT(`".$sIndexColumn."`)
FROM $sTable
";
$rResultTotal = sqlsrv_query($conn, $sQuery, $params, $options);
$aResultTotal = sqlsrv_fetch_array($rResultTotal, SQLSRV_FETCH_ASSOC);
$iTotal = $aResultTotal[0];
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = sqlsrv_fetch_array($rResult, SQLSRV_FETCH_ASSOC) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
/* Special output formatting for 'version' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
?>
但是,它不起作用。我不知道为什么。请帮助我,谢谢你,克里斯