PageRenderTime 78ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

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

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