PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/js/DataTables-1.9.1/examples/server_side/scripts/post.php

https://bitbucket.org/veroreinah/bookworm
PHP | 184 lines | 111 code | 27 blank | 46 comment | 26 complexity | e0caa8bea03b09da19769e7a47c7c5d7 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  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. * MySQL connection
  26. */
  27. $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
  28. die( 'Could not open connection to server' );
  29. mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
  30. die( 'Could not select database '. $gaSql['db'] );
  31. /*
  32. * Paging
  33. */
  34. $sLimit = "";
  35. if ( isset( $_POST['iDisplayStart'] ) && $_POST['iDisplayLength'] != '-1' )
  36. {
  37. $sLimit = "LIMIT ".mysql_real_escape_string( $_POST['iDisplayStart'] ).", ".
  38. mysql_real_escape_string( $_POST['iDisplayLength'] );
  39. }
  40. /*
  41. * Ordering
  42. */
  43. if ( isset( $_POST['iSortCol_0'] ) )
  44. {
  45. $sOrder = "ORDER BY ";
  46. for ( $i=0 ; $i<intval( $_POST['iSortingCols'] ) ; $i++ )
  47. {
  48. if ( $_POST[ 'bSortable_'.intval($_POST['iSortCol_'.$i]) ] == "true" )
  49. {
  50. $sOrder .= $aColumns[ intval( $_POST['iSortCol_'.$i] ) ]."
  51. ".mysql_real_escape_string( $_POST['sSortDir_'.$i] ) .", ";
  52. }
  53. }
  54. $sOrder = substr_replace( $sOrder, "", -2 );
  55. if ( $sOrder == "ORDER BY" )
  56. {
  57. $sOrder = "";
  58. }
  59. }
  60. /*
  61. * Filtering
  62. * NOTE this does not match the built-in DataTables filtering which does it
  63. * word by word on any field. It's possible to do here, but concerned about efficiency
  64. * on very large tables, and MySQL's regex functionality is very limited
  65. */
  66. $sWhere = "";
  67. if ( $_POST['sSearch'] != "" )
  68. {
  69. $sWhere = "WHERE (";
  70. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  71. {
  72. $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_POST['sSearch'] )."%' OR ";
  73. }
  74. $sWhere = substr_replace( $sWhere, "", -3 );
  75. $sWhere .= ')';
  76. }
  77. /* Individual column filtering */
  78. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  79. {
  80. if ( $_POST['bSearchable_'.$i] == "true" && $_POST['sSearch_'.$i] != '' )
  81. {
  82. if ( $sWhere == "" )
  83. {
  84. $sWhere = "WHERE ";
  85. }
  86. else
  87. {
  88. $sWhere .= " AND ";
  89. }
  90. $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_POST['sSearch_'.$i])."%' ";
  91. }
  92. }
  93. /*
  94. * SQL queries
  95. * Get data to display
  96. */
  97. $sQuery = "
  98. SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
  99. FROM $sTable
  100. $sWhere
  101. $sOrder
  102. $sLimit
  103. ";
  104. $rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
  105. /* Data set length after filtering */
  106. $sQuery = "
  107. SELECT FOUND_ROWS()
  108. ";
  109. $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
  110. $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
  111. $iFilteredTotal = $aResultFilterTotal[0];
  112. /* Total data set length */
  113. $sQuery = "
  114. SELECT COUNT(".$sIndexColumn.")
  115. FROM $sTable
  116. ";
  117. $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
  118. $aResultTotal = mysql_fetch_array($rResultTotal);
  119. $iTotal = $aResultTotal[0];
  120. /*
  121. * Output
  122. */
  123. $sOutput = '{';
  124. $sOutput .= '"sEcho": '.intval($_POST['sEcho']).', ';
  125. $sOutput .= '"iTotalRecords": '.$iTotal.', ';
  126. $sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
  127. $sOutput .= '"aaData": [ ';
  128. while ( $aRow = mysql_fetch_array( $rResult ) )
  129. {
  130. $sOutput .= "[";
  131. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  132. {
  133. if ( $aColumns[$i] == "version" )
  134. {
  135. /* Special output formatting for 'version' */
  136. $sOutput .= ($aRow[ $aColumns[$i] ]=="0") ?
  137. '"-",' :
  138. '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
  139. }
  140. else if ( $aColumns[$i] != ' ' )
  141. {
  142. /* General output */
  143. $sOutput .= '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
  144. }
  145. }
  146. /*
  147. * Optional Configuration:
  148. * If you need to add any extra columns (add/edit/delete etc) to the table, that aren't in the
  149. * database - you can do it here
  150. */
  151. $sOutput = substr_replace( $sOutput, "", -1 );
  152. $sOutput .= "],";
  153. }
  154. $sOutput = substr_replace( $sOutput, "", -1 );
  155. $sOutput .= '] }';
  156. echo $sOutput;
  157. ?>