PageRenderTime 72ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/DataTables-1.8.1/examples/server_side/scripts/objects.php

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