PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/veroreinah/bookworm
PHP | 122 lines | 102 code | 13 blank | 7 comment | 29 complexity | 353fa000d645a77ff2af8af6de6fe073 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /* MySQL connection */
  3. include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" ); /* ;-) */
  4. $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
  5. die( 'Could not open connection to server' );
  6. mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
  7. die( 'Could not select database '. $gaSql['db'] );
  8. /* Paging */
  9. $sLimit = "";
  10. if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
  11. {
  12. $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
  13. mysql_real_escape_string( $_GET['iDisplayLength'] );
  14. }
  15. /* Ordering */
  16. if ( isset( $_GET['iSortCol_0'] ) )
  17. {
  18. $sOrder = "ORDER BY ";
  19. for ( $i=0 ; $i<mysql_real_escape_string( $_GET['iSortingCols'] ) ; $i++ )
  20. {
  21. $sOrder .= fnColumnToField(mysql_real_escape_string( $_GET['iSortCol_'.$i] ))."
  22. ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
  23. }
  24. $sOrder = substr_replace( $sOrder, "", -2 );
  25. }
  26. /* Filtering - NOTE this does not match the built-in DataTables filtering which does it
  27. * word by word on any field. It's possible to do here, but concerned about efficiency
  28. * on very large tables, and MySQL's regex functionality is very limited
  29. */
  30. $sWhere = "";
  31. if ( $_GET['sSearch'] != "" )
  32. {
  33. $sWhere = "WHERE ( engine LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ".
  34. "browser LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ".
  35. "platform LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ".
  36. "version LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ".
  37. "grade LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' )";
  38. }
  39. for ( $i=0 ; $i<$_GET['iColumns'] ; $i++ )
  40. {
  41. if ( $_GET['sSearch_'.$i] != '' )
  42. {
  43. if ( $sWhere != "" )
  44. {
  45. $sWhere .= " AND ";
  46. }
  47. else
  48. {
  49. $sWhere .= "WHERE ";
  50. }
  51. $sWhere .= fnColumnToField($i) ." LIKE '%".mysql_real_escape_string( $_GET['sSearch_'.$i] )."%'";
  52. }
  53. }
  54. $sQuery = "
  55. SELECT SQL_CALC_FOUND_ROWS id, engine, browser, platform, version, grade
  56. FROM ajax
  57. $sWhere
  58. $sOrder
  59. $sLimit
  60. ";
  61. $rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
  62. $sQuery = "
  63. SELECT FOUND_ROWS()
  64. ";
  65. $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
  66. $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
  67. $iFilteredTotal = $aResultFilterTotal[0];
  68. $sQuery = "
  69. SELECT COUNT(id)
  70. FROM ajax
  71. ";
  72. $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
  73. $aResultTotal = mysql_fetch_array($rResultTotal);
  74. $iTotal = $aResultTotal[0];
  75. $sOutput = '{';
  76. $sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
  77. $sOutput .= '"iTotalRecords": '.$iTotal.', ';
  78. $sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
  79. $sOutput .= '"aaData": [ ';
  80. while ( $aRow = mysql_fetch_array( $rResult ) )
  81. {
  82. $sOutput .= "[";
  83. $sOutput .= '"'.str_replace('"', '\"', $aRow['engine']).'",';
  84. $sOutput .= '"'.str_replace('"', '\"', $aRow['browser']).'",';
  85. $sOutput .= '"'.str_replace('"', '\"', $aRow['platform']).'",';
  86. if ( $aRow['version'] == "0" )
  87. $sOutput .= '"-",';
  88. else
  89. $sOutput .= '"'.str_replace('"', '\"', $aRow['version']).'",';
  90. $sOutput .= '"'.str_replace('"', '\"', $aRow['grade']).'"';
  91. $sOutput .= "],";
  92. }
  93. $sOutput = substr_replace( $sOutput, "", -1 );
  94. $sOutput .= '] }';
  95. echo $sOutput;
  96. function fnColumnToField( $i )
  97. {
  98. if ( $i == 0 )
  99. return "engine";
  100. else if ( $i == 1 )
  101. return "browser";
  102. else if ( $i == 2 )
  103. return "platform";
  104. else if ( $i == 3 )
  105. return "version";
  106. else if ( $i == 4 )
  107. return "grade";
  108. }
  109. ?>