infiniteScroll.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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( 'name', 'phone', 'email', 'city', 'zip' );
  9. /* Indexed column (used for fast and accurate table cardinality) */
  10. $sIndexColumn = "id";
  11. /* DB table to use */
  12. $sTable = "testData";
  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['iStart'] ) && isset( $_GET['iLength'] ) )
  36. {
  37. $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iStart'] ).", ".
  38. mysql_real_escape_string( $_GET['iLength'] );
  39. }
  40. else
  41. {
  42. echo '{ "aaData": [] }';
  43. exit();
  44. }
  45. /*
  46. * SQL queries
  47. * Get data to display
  48. */
  49. $sQuery = "
  50. SELECT ".str_replace(" , ", " ", implode(", ", $aColumns))."
  51. FROM $sTable
  52. ORDER BY name ASC
  53. $sLimit
  54. ";
  55. $rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
  56. /*
  57. * Output
  58. */
  59. $sOutput = '{';
  60. $sOutput .= '"aaData": [ ';
  61. while ( $aRow = mysql_fetch_array( $rResult ) )
  62. {
  63. $sOutput .= "[";
  64. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  65. {
  66. /* General output */
  67. $sOutput .= '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
  68. }
  69. /*
  70. * Optional Configuration:
  71. * If you need to add any extra columns (add/edit/delete etc) to the table, that aren't in the
  72. * database - you can do it here
  73. */
  74. $sOutput = substr_replace( $sOutput, "", -1 );
  75. $sOutput .= "],";
  76. }
  77. $sOutput = substr_replace( $sOutput, "", -1 );
  78. $sOutput .= '] }';
  79. echo $sOutput;
  80. ?>