post.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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( $_POST['iDisplayStart'] ) && $_POST['iDisplayLength'] != '-1' )
  48. {
  49. $sLimit = "LIMIT ".mysql_real_escape_string( $_POST['iDisplayStart'] ).", ".
  50. mysql_real_escape_string( $_POST['iDisplayLength'] );
  51. }
  52. /*
  53. * Ordering
  54. */
  55. if ( isset( $_POST['iSortCol_0'] ) )
  56. {
  57. $sOrder = "ORDER BY ";
  58. for ( $i=0 ; $i<intval( $_POST['iSortingCols'] ) ; $i++ )
  59. {
  60. if ( $_POST[ 'bSortable_'.intval($_POST['iSortCol_'.$i]) ] == "true" )
  61. {
  62. $sOrder .= $aColumns[ intval( $_POST['iSortCol_'.$i] ) ]."
  63. ".mysql_real_escape_string( $_POST['sSortDir_'.$i] ) .", ";
  64. }
  65. }
  66. $sOrder = substr_replace( $sOrder, "", -2 );
  67. if ( $sOrder == "ORDER BY" )
  68. {
  69. $sOrder = "";
  70. }
  71. }
  72. /*
  73. * Filtering
  74. * NOTE this does not match the built-in DataTables filtering which does it
  75. * word by word on any field. It's possible to do here, but concerned about efficiency
  76. * on very large tables, and MySQL's regex functionality is very limited
  77. */
  78. $sWhere = "";
  79. if ( $_POST['sSearch'] != "" )
  80. {
  81. $sWhere = "WHERE (";
  82. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  83. {
  84. if ( isset($_POST['bSearchable_'.$i]) && $_POST['bSearchable_'.$i] == "true" )
  85. {
  86. $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_POST['sSearch'] )."%' OR ";
  87. }
  88. }
  89. $sWhere = substr_replace( $sWhere, "", -3 );
  90. $sWhere .= ')';
  91. }
  92. /* Individual column filtering */
  93. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  94. {
  95. if ( $_POST['bSearchable_'.$i] == "true" && $_POST['sSearch_'.$i] != '' )
  96. {
  97. if ( $sWhere == "" )
  98. {
  99. $sWhere = "WHERE ";
  100. }
  101. else
  102. {
  103. $sWhere .= " AND ";
  104. }
  105. $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_POST['sSearch_'.$i])."%' ";
  106. }
  107. }
  108. /*
  109. * SQL queries
  110. * Get data to display
  111. */
  112. $sQuery = "
  113. SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
  114. FROM $sTable
  115. $sWhere
  116. $sOrder
  117. $sLimit
  118. ";
  119. $rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  120. /* Data set length after filtering */
  121. $sQuery = "
  122. SELECT FOUND_ROWS()
  123. ";
  124. $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  125. $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
  126. $iFilteredTotal = $aResultFilterTotal[0];
  127. /* Total data set length */
  128. $sQuery = "
  129. SELECT COUNT(".$sIndexColumn.")
  130. FROM $sTable
  131. ";
  132. $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  133. $aResultTotal = mysql_fetch_array($rResultTotal);
  134. $iTotal = $aResultTotal[0];
  135. /*
  136. * Output
  137. */
  138. $sOutput = '{';
  139. $sOutput .= '"sEcho": '.intval($_POST['sEcho']).', ';
  140. $sOutput .= '"iTotalRecords": '.$iTotal.', ';
  141. $sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
  142. $sOutput .= '"aaData": [ ';
  143. while ( $aRow = mysql_fetch_array( $rResult ) )
  144. {
  145. $sOutput .= "[";
  146. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  147. {
  148. if ( $aColumns[$i] == "version" )
  149. {
  150. /* Special output formatting for 'version' */
  151. $sOutput .= ($aRow[ $aColumns[$i] ]=="0") ?
  152. '"-",' :
  153. '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
  154. }
  155. else if ( $aColumns[$i] != ' ' )
  156. {
  157. /* General output */
  158. $sOutput .= '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
  159. }
  160. }
  161. /*
  162. * Optional Configuration:
  163. * If you need to add any extra columns (add/edit/delete etc) to the table, that aren't in the
  164. * database - you can do it here
  165. */
  166. $sOutput = substr_replace( $sOutput, "", -1 );
  167. $sOutput .= "],";
  168. }
  169. $sOutput = substr_replace( $sOutput, "", -1 );
  170. $sOutput .= '] }';
  171. echo $sOutput;
  172. ?>