我有一个jQuery数据表的问题我想使用服务器端处理,因为需要加载大量数据。但从我已经阅读的文档中使用本机php,我使用codeigniter 3.修改代码非常困难。这是我在控制器中使用的代码
public function dataTable()
{
$sIndexColumn = "";
$sTable = "myTable";
$gaSql['user'] = "test";
$gaSql['password'] = "t3st";
$gaSql['db'] = "myDatabase";
$gaSql['server'] = "Driver={SQL Server Native Client 10.0};Server=ITI-0299-PC\JTSMSSQLSERVER;Port=1326;Database=myDatabase; Uid=test;Pwd=t3st;"; //Locale
$aColumns = array(
'id','nmrumahsakit','alamat','kota',
'provinsi','rawat_inap','rawat_jalan',
'mcu','telp','fax','latitude',
'longitude','created_at','updated_at');
/*
* ODBC connection
*/
$connectionInfo = array("UID" => $gaSql['user'], "PWD" => $gaSql['password'], "Database"=>$gaSql['db'],"ReturnDatesAsStrings"=>true);
$gaSql['link'] = sqlsrv_connect( $gaSql['server'], $connectionInfo);
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
/* Ordering */
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) ) {
$sOrder = "ORDER BY id ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ ) {
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" ) {
$sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
".addslashes( $_GET['sSortDir_'.$i] ) .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY id" ) {
$sOrder = "";
}
}
/* Filtering */
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" ) {
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
$sWhere .= $aColumns[$i]." LIKE '%".addslashes( $_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 '%".addslashes($_GET['sSearch_'.$i])."%' ";
}
}
/* Paging */
$top = (isset($_GET['iDisplayStart']))?((int)$_GET['iDisplayStart']):0 ;
$limit = (isset($_GET['iDisplayLength']))?((int)$_GET['iDisplayLength'] ):10;
$sQuery = "SELECT TOP $limit ".implode(",",$aColumns)."
FROM $sTable
$sWhere ".(($sWhere=="")?" WHERE ":" AND ")." $sIndexColumn NOT IN
(
SELECT $sIndexColumn FROM
(
SELECT TOP $top ".implode(",",$aColumns)."
FROM $sTable
$sWhere
$sOrder
)
as [virtTable]
)
$sOrder";
$rResult = sqlsrv_query($gaSql['link'],$sQuery) or die("$sQuery: " . sqlsrv_errors());
$sQueryCnt = "SELECT * FROM $sTable $sWhere";
$rResultCnt = sqlsrv_query( $gaSql['link'], $sQueryCnt ,$params, $options) or die (" $sQueryCnt: " . sqlsrv_errors());
$iFilteredTotal = sqlsrv_num_rows( $rResultCnt );
$sQuery = " SELECT * FROM $sTable ";
$rResultTotal = sqlsrv_query( $gaSql['link'], $sQuery ,$params, $options) or die(sqlsrv_errors());
$iTotal = sqlsrv_num_rows( $rResultTotal );
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = sqlsrv_fetch_array( $rResult ) ) {
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
if ( $aColumns[$i] != ' ' ) {
$v = $aRow[ $aColumns[$i] ];
$v = mb_check_encoding($v, 'UTF-8') ? $v : utf8_encode($v);
$row[]=$v;
}
}
If (!empty($row)) { $output['aaData'][] = $row; }
}
echo json_encode( $output );
}
这是数据表的js配置:
$('#datatable2').dataTable({
"sScrollY": "400px",
"bProcessing": true,
"bServerSide": true,
"sServerMethod": "GET",
"sAjaxSource": "<?php echo base_url(); ?>mycontroller/mymethod",
"iDisplayLength": 10,
"aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
"aaSorting": [[0, 'asc']],
"aoColumns": [
{ "bVisible": true, "bSearchable": true, "bSortable": true },
{ "bVisible": true, "bSearchable": true, "bSortable": true },
{ "bVisible": true, "bSearchable": true, "bSortable": true }
]
}).fnSetFilteringDelay(700);
});
这是观点:
<table id="datatable2" class="table table-bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Nama R.S</th>
<th>Alamat</th>
<th>Kota</th>
<th>Provinsi</th>
<th>Rawat Inap</th>
<th>Rawat Jalan</th>
<th>MCU</th>
<th>No.Telp</th>
<th>No.Fax</th>
<th>Latitude</th>
<th>Longitude</th>
<th>Created At</th>
<th>Updated At</th>
<th>Menu</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
经过测试后,我得到了这个错误。
遇到PHP错误
严重性:错误
消息:调用未定义的函数sqlsrv_connect()
文件名:controllers / Provider.php
行号:302
回溯:
我已经在config / database.php中使用了连接 我的问题是如何在codeigniter中使用服务器端处理?
上面的问题解决了,但是我遇到了js的新问题。
DataTables警告(table id ='datatable2'):DataTables警告:JSON 无法解析来自服务器的数据。这是由JSON引起的 格式化错误。
CMIIW
答案 0 :(得分:0)
您的PHP安装似乎缺少允许您使用sqlsrv_connect函数的扩展。
确保在php.ini中加载了php_sqlsrv_XX_ts.dll扩展名。