objects.php 5.0 KB

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