2017-10-05 88 views
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 joinwhere條款。

我想將此請求轉換爲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.idnamecharacters.id=ucp_request.characteridaccountcharacters.userid=users.idtime被格式化ucp_request.create_timeaction和與ucp_request.admin_timeucp_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添加到$sWherestatus$aColumns數組,條款工作,但在action輸出1)。也無法想象如何在那裏使用LEFT JOIN和多個where子句。

回答

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 
+0

試過了,它沒有返回了'name'和'表中的account'列。 –