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

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

https://bitbucket.org/veroreinah/bookworm
PHP | 179 lines | 110 code | 27 blank | 42 comment | 28 complexity | 206ec8dd9b5bb2417954264fd7ddd7ed 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 id, ".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. // Add the row ID and class to the object
  134. $row['DT_RowId'] = 'row_'.$aRow['id'];
  135. $row['DT_RowClass'] = 'grade'.$aRow['grade'];
  136. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  137. {
  138. if ( $aColumns[$i] == "version" )
  139. {
  140. /* Special output formatting for 'version' column */
  141. $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
  142. }
  143. else if ( $aColumns[$i] != ' ' )
  144. {
  145. /* General output */
  146. $row[] = $aRow[ $aColumns[$i] ];
  147. }
  148. }
  149. $output['aaData'][] = $row;
  150. }
  151. echo json_encode( $output );
  152. ?>