/webportal/wordpress/wp-content/plugins/table-display-form-for-fresta/fetch_data_from_server.php

https://github.com/sics-sse/moped · PHP · 176 lines · 106 code · 25 blank · 45 comment · 26 complexity · 6ed894c253e796cf35d959729a095c3e MD5 · raw file

  1. <?php
  2. /*
  3. * Script: DataTables server-side script for PHP and MySQL
  4. * Copyright: 2010 - Allan Jardine
  5. * License: GPL v2 or BSD (3-point)
  6. */
  7. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  8. * Easy set variables
  9. */
  10. /* Array of database columns which should be read and sent back to DataTables. Use a space where
  11. * you want to insert a non-database field (for example a counter or static image)
  12. */
  13. $aColumns = array( 'applicationName', 'publisher', 'version', 'plugins' );
  14. /* Indexed column (used for fast and accurate table cardinality) */
  15. $sIndexColumn = "id";
  16. /* DB table to use */
  17. $sTable = "application";
  18. /* Database connection information */
  19. $gaSql['user'] = "root";
  20. $gaSql['password'] = "fresta";
  21. $gaSql['db'] = "fresta";
  22. $gaSql['server'] = "localhost";
  23. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  24. * If you just want to use the basic configuration for DataTables with PHP server-side, there is
  25. * no need to edit below this line
  26. */
  27. /*
  28. * MySQL connection
  29. */
  30. $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
  31. die( 'Could not open connection to server' );
  32. mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
  33. die( 'Could not select database '. $gaSql['db'] );
  34. /*
  35. * Paging
  36. */
  37. $sLimit = "";
  38. if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
  39. {
  40. $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
  41. mysql_real_escape_string( $_GET['iDisplayLength'] );
  42. }
  43. /*
  44. * Ordering
  45. */
  46. if ( isset( $_GET['iSortCol_0'] ) )
  47. {
  48. $sOrder = "ORDER BY ";
  49. for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
  50. {
  51. if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
  52. {
  53. $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
  54. ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
  55. }
  56. }
  57. $sOrder = substr_replace( $sOrder, "", -2 );
  58. if ( $sOrder == "ORDER BY" )
  59. {
  60. $sOrder = "";
  61. }
  62. }
  63. /*
  64. * Filtering
  65. * NOTE this does not match the built-in DataTables filtering which does it
  66. * word by word on any field. It's possible to do here, but concerned about efficiency
  67. * on very large tables, and MySQL's regex functionality is very limited
  68. */
  69. $sWhere = "";
  70. if ( $_GET['sSearch'] != "" )
  71. {
  72. $sWhere = "WHERE (";
  73. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  74. {
  75. $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
  76. }
  77. $sWhere = substr_replace( $sWhere, "", -3 );
  78. $sWhere .= ')';
  79. }
  80. /* Individual column filtering */
  81. for ( $i=0 ; $i<count($aColumns) ; $i++ )
  82. {
  83. if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
  84. {
  85. if ( $sWhere == "" )
  86. {
  87. $sWhere = "WHERE ";
  88. }
  89. else
  90. {
  91. $sWhere .= " AND ";
  92. }
  93. $sWhere .= $aColumns[$i]." 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[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
  141. }
  142. else if ( $aColumns[$i] != ' ' )
  143. {
  144. /* General output */
  145. $row[] = $aRow[ $aColumns[$i] ];
  146. }
  147. }
  148. $output['aaData'][] = $row;
  149. }
  150. echo json_encode( $output );
  151. ?>