Guest User

Untitled

a guest
Oct 4th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. <?php
  2. /*
  3. * Script: DataTables server-side script for PHP and MySQL
  4. * Copyright: 2010 - Allan Jardine, 2012 - Chris Wright
  5. * License: GPL v2 or BSD (3-point)
  6. */
  7.  
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  9. * Easy set variables
  10. */
  11.  
  12. /* Array of database columns which should be read and sent back to DataTables. Use a space where
  13. * you want to insert a non-database field (for example a counter or static image)
  14. */
  15. $aColumns = array( 'id', 'characterid', 'userid', 'create_time', 'status');
  16.  
  17. /* Indexed column (used for fast and accurate table cardinality) */
  18. $sIndexColumn = "id";
  19.  
  20. /* DB table to use */
  21. $sTable = "ucp_request";
  22.  
  23. /* Database connection information */
  24. $gaSql['user'] = "root";
  25. $gaSql['password'] = "";
  26. $gaSql['db'] = "base";
  27. $gaSql['server'] = "localhost";
  28.  
  29.  
  30. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  31. * If you just want to use the basic configuration for DataTables with PHP server-side, there is
  32. * no need to edit below this line
  33. */
  34.  
  35. /*
  36. * Local functions
  37. */
  38. function fatal_error ( $sErrorMessage = '' )
  39. {
  40. header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
  41. die( $sErrorMessage );
  42. }
  43.  
  44.  
  45. /*
  46. * MySQL connection
  47. */
  48. if ( ! $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) )
  49. {
  50. fatal_error( 'Could not open connection to server' );
  51. }
  52.  
  53. if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) )
  54. {
  55. fatal_error( 'Could not select database ' );
  56. }
  57.  
  58.  
  59. /*
  60. * Paging
  61. */
  62. $sLimit = "";
  63. if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
  64. {
  65. $sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
  66. intval( $_GET['iDisplayLength'] );
  67. }
  68.  
  69.  
  70. /*
  71. * Ordering
  72. */
  73. $sOrder = "";
  74. if ( isset( $_GET['iSortCol_0'] ) )
  75. {
  76. $sOrder = "ORDER BY ";
  77. for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
  78. {
  79. if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
  80. {
  81. $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
  82. ".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
  83. }
  84. }
  85.  
  86. $sOrder = substr_replace( $sOrder, "", -2 );
  87. if ( $sOrder == "ORDER BY" )
  88. {
  89. $sOrder = "";
  90. }
  91. }
  92.  
  93.  
  94. /*
  95. * Filtering
  96. * NOTE this does not match the built-in DataTables filtering which does it
  97. * word by word on any field. It's possible to do here, but concerned about efficiency
  98. * on very large tables, and MySQL's regex functionality is very limited
  99. */
  100. $sWhere = "status=1";
  101. if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
  102. {
  103. $sWhere = "WHERE (";
  104. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  105. {
  106. if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
  107. {
  108. $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
  109. }
  110. }
  111. $sWhere = substr_replace( $sWhere, "", -3 );
  112. $sWhere .= ')';
  113. }
  114.  
  115. /* Individual column filtering */
  116. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  117. {
  118. if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
  119. {
  120. if ( $sWhere == "" )
  121. {
  122. $sWhere = "WHERE ";
  123. }
  124. else
  125. {
  126. $sWhere .= " AND ";
  127. }
  128. $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
  129. }
  130. }
  131.  
  132.  
  133. /*
  134. * SQL queries
  135. * Get data to display
  136. */
  137. $sQuery = "
  138. SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
  139. FROM $sTable
  140. WHERE $sWhere
  141. $sOrder
  142. $sLimit
  143. ";
  144. $rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  145.  
  146. /* Data set length after filtering */
  147. $sQuery = "
  148. SELECT FOUND_ROWS()
  149. ";
  150. $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  151. $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
  152. $iFilteredTotal = $aResultFilterTotal[0];
  153.  
  154. /* Total data set length */
  155. $sQuery = "
  156. SELECT COUNT(".$sIndexColumn.")
  157. FROM $sTable
  158. ";
  159. $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  160. $aResultTotal = mysql_fetch_array($rResultTotal);
  161. $iTotal = $aResultTotal[0];
  162.  
  163.  
  164. /*
  165. * Output
  166. */
  167. $output = array(
  168. "sEcho" => intval($_GET['sEcho']),
  169. "iTotalRecords" => $iTotal,
  170. "iTotalDisplayRecords" => $iFilteredTotal,
  171. "aaData" => array()
  172. );
  173.  
  174. while ( $aRow = mysql_fetch_array( $rResult ) )
  175. {
  176. $row = array();
  177. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  178. {
  179. if ( $aColumns[$i] == "version" )
  180. {
  181. $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
  182. }
  183. else if ( $aColumns[$i] != ' ' )
  184. {
  185. $row[] = $aRow[ $aColumns[$i] ];
  186. }
  187. }
  188. $output['aaData'][] = $row;
  189. }
  190.  
  191. echo json_encode( $output );
  192. ?>
Advertisement
Add Comment
Please, Sign In to add comment