PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/admin-dashboard/assets/advanced-datatable/examples/server_side/scripts/id_jsonp.php

https://gitlab.com/emad.rashad/mls-grading
PHP | 196 lines | 122 code | 29 blank | 45 comment | 33 complexity | 965001bff45efd5264d8004b1755e8c3 MD5 | raw file
  1. <?php
  2. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  3. * Easy set variables
  4. */
  5. /* Array of database columns which should be read and sent back to DataTables. Use a space where
  6. * you want to insert a non-database field (for example a counter or static image)
  7. */
  8. $aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );
  9. /* Indexed column (used for fast and accurate table cardinality) */
  10. $sIndexColumn = "id";
  11. /* DB table to use */
  12. $sTable = "ajax";
  13. /* Database connection information */
  14. $gaSql['user'] = "";
  15. $gaSql['password'] = "";
  16. $gaSql['db'] = "";
  17. $gaSql['server'] = "localhost";
  18. /* REMOVE THIS LINE (it just includes my SQL connection user/pass) */
  19. include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" );
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  21. * If you just want to use the basic configuration for DataTables with PHP server-side, there is
  22. * no need to edit below this line
  23. */
  24. /*
  25. * Local functions
  26. */
  27. function fatal_error ( $sErrorMessage = '' )
  28. {
  29. header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
  30. die( $sErrorMessage );
  31. }
  32. /*
  33. * MySQL connection
  34. */
  35. if ( ! $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) )
  36. {
  37. fatal_error( 'Could not open connection to server' );
  38. }
  39. if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) )
  40. {
  41. fatal_error( 'Could not select database ' );
  42. }
  43. /*
  44. * Paging
  45. */
  46. $sLimit = "";
  47. if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
  48. {
  49. $sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
  50. intval( $_GET['iDisplayLength'] );
  51. }
  52. /*
  53. * Ordering
  54. */
  55. $sOrder = "";
  56. if ( isset( $_GET['iSortCol_0'] ) )
  57. {
  58. $sOrder = "ORDER BY ";
  59. for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
  60. {
  61. if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
  62. {
  63. $sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
  64. ($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
  65. }
  66. }
  67. $sOrder = substr_replace( $sOrder, "", -2 );
  68. if ( $sOrder == "ORDER BY" )
  69. {
  70. $sOrder = "";
  71. }
  72. }
  73. /*
  74. * Filtering
  75. * NOTE this does not match the built-in DataTables filtering which does it
  76. * word by word on any field. It's possible to do here, but concerned about efficiency
  77. * on very large tables, and MySQL's regex functionality is very limited
  78. */
  79. $sWhere = "";
  80. if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
  81. {
  82. $sWhere = "WHERE (";
  83. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  84. {
  85. if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
  86. {
  87. $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
  88. }
  89. }
  90. $sWhere = substr_replace( $sWhere, "", -3 );
  91. $sWhere .= ')';
  92. }
  93. /* Individual column filtering */
  94. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  95. {
  96. if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
  97. {
  98. if ( $sWhere == "" )
  99. {
  100. $sWhere = "WHERE ";
  101. }
  102. else
  103. {
  104. $sWhere .= " AND ";
  105. }
  106. $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
  107. }
  108. }
  109. /*
  110. * SQL queries
  111. * Get data to display
  112. */
  113. $sQuery = "
  114. SELECT SQL_CALC_FOUND_ROWS id, ".str_replace(" , ", " ", implode(", ", $aColumns))."
  115. FROM $sTable
  116. $sWhere
  117. $sOrder
  118. $sLimit
  119. ";
  120. $rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  121. /* Data set length after filtering */
  122. $sQuery = "
  123. SELECT FOUND_ROWS()
  124. ";
  125. $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  126. $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
  127. $iFilteredTotal = $aResultFilterTotal[0];
  128. /* Total data set length */
  129. $sQuery = "
  130. SELECT COUNT(".$sIndexColumn.")
  131. FROM $sTable
  132. ";
  133. $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  134. $aResultTotal = mysql_fetch_array($rResultTotal);
  135. $iTotal = $aResultTotal[0];
  136. /*
  137. * Output
  138. */
  139. $output = array(
  140. "sEcho" => intval($_GET['sEcho']),
  141. "iTotalRecords" => $iTotal,
  142. "iTotalDisplayRecords" => $iFilteredTotal,
  143. "aaData" => array()
  144. );
  145. while ( $aRow = mysql_fetch_array( $rResult ) )
  146. {
  147. $row = array();
  148. // Add the row ID and class to the object
  149. $row['DT_RowId'] = 'row_'.$aRow['id'];
  150. $row['DT_RowClass'] = 'grade'.$aRow['grade'];
  151. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  152. {
  153. if ( $aColumns[$i] == "version" )
  154. {
  155. /* Special output formatting for 'version' column */
  156. $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
  157. }
  158. else if ( $aColumns[$i] != ' ' )
  159. {
  160. /* General output */
  161. $row[] = $aRow[ $aColumns[$i] ];
  162. }
  163. }
  164. $output['aaData'][] = $row;
  165. }
  166. echo $_GET['callback'].'('.json_encode( $output ).');';
  167. ?>