使用干净的php我使用了这个请求:
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
格式为{{1} {}}和ucp_request.create_time
是action
,ucp_request.admin_time
使用的按钮。
所以我尝试了这个:
HTML PAGE:
ucp_request.admin_time_id
服务器-RESPONCE.PHP:
...
<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>
在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 );
?>
子句(如果我将where
添加到status=1
和$sWhere
到status
数组,则子句有效,但在$aColumns
输出1)。也无法想象如何在那里使用action
和多个LEFT JOIN
条款。
答案 0 :(得分:0)
Mysql的简单查询:
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) AS admin_login,
characters.name,
users.login
FROM
ucp_request, characters, users
WHERE
characters.id=ucp_request.characterid
AND
characters.userid=users.id
AND
ucp_request.status=1