0
用干净的PHP我用这个请求:转换SQL请求到数据表类型请求
SELECT ucp_request.id, ucp_request.admin_time, ucp_request.admin_time_id,
ucp_request.create_time,(SELECT login FROM users WHERE
id=ucp_request.admin_time_id), characters.name, users.login FROM ucp_request
LEFT JOIN characters ON characters.id=ucp_request.characterid LEFT JOIN users
ON characters.userid=users.id WHERE ucp_request.status=1
正如你可以看到,有很多left join
和where
条款。
我想将此请求转换为datatables.net类型的请求。它有困难的服务器端处理结构,我无法理解如何做到这一点。表(实施例)的
结构:
№ || Name || Account || Time || Action
--------------------------------------------------------
1 || John_Johnson || John || 01.01 01:01 || Check
2 || William_Smith || Will || 01.02 05:15 || Check
其中№
是ucp_request.id
,name
是characters.id=ucp_request.characterid
,account
是characters.userid=users.id
,time
被格式化ucp_request.create_time
action
和与ucp_request.admin_time
,ucp_request.admin_time_id
使用的按钮。
所以,我想这一点:
HTML页面:
...
<div class="material-datatables">
<table id="ucprequests" class="table table-striped table-no-bordered table-hover"
cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th>№</th>
<th>Name</th>
<th>Account</th>
<th>Time</th>
<th class="disabled-sorting text-right">Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>№</th>
<th>Name</th>
<th>Account</th>
<th>Time</th>
<th class="text-right">Action</th>
</tr>
</tfoot>
<tbody>
<tr>
<td colspan="6" class="dataTables_empty">Loading</td>
</tr>
</tbody>
</table>
</div>
...
<script src="../assets/js/jquery.datatables.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#ucprequests').DataTable({
"aProcessing": true,
"aServerSide": true,
"ajax": "../admin/server-responce.php",
responsive: true,
"order": [[ 1, "asc" ]]
});
var table = $('#datatables').DataTable();
});
</script>
SERVER-RESPONCE.PHP:
<?php
/*
* Script: DataTables server-side script for PHP and MySQL
* Copyright: 2010 - Allan Jardine, 2012 - Chris Wright
* License: GPL v2 or BSD (3-point)
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array('id', 'characterid', 'userid', 'create_time');
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "id";
/* DB table to use */
$sTable = "ucp_request";
/* Database connection information */
$gaSql['user'] = "root";
$gaSql['password'] = "";
$gaSql['db'] = "database";
$gaSql['server'] = "localhost";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* Local functions
*/
function fatal_error ($sErrorMessage = '')
{
header($_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error');
die($sErrorMessage);
}
/*
* MySQL connection
*/
if (! $gaSql['link'] = mysql_pconnect($gaSql['server'], $gaSql['user'], $gaSql['password'] ))
{
fatal_error('Could not open connection to server');
}
if (! mysql_select_db($gaSql['db'], $gaSql['link']))
{
fatal_error('Could not select database ');
}
/*
* 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++)
{
if (isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true")
{
$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])."%' ";
}
}
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query($sQuery, $gaSql['link']) or fatal_error('MySQL Error: ' . mysql_errno());
/* Data set length after filtering */
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query($sQuery, $gaSql['link']) or fatal_error('MySQL Error: ' . mysql_errno());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT(".$sIndexColumn.")
FROM $sTable
";
$rResultTotal = mysql_query($sQuery, $gaSql['link']) or fatal_error('MySQL Error: ' . mysql_errno());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ($aRow = mysql_fetch_array($rResult))
{
$row = array();
for ($i=0 ; $i<count($aColumns) ; $i++)
{
if ($aColumns[$i] == "version")
{
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ($aColumns[$i] != ' ')
{
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode($output);
?>
在PHP文件,我不能对付where
子句(如果我将status=1
添加到$sWhere
和status
到$aColumns
数组,条款工作,但在action
输出1)。也无法想象如何在那里使用LEFT JOIN
和多个where
子句。
试过了,它没有返回了'name'和'表中的account'列。 –