Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HeuristicLabWeb.PluginHost/HLWebPluginHost/Content/jQuery/jQueryPlugins/DataTables-1.7.6/examples/examples_support/server_processing_filter_col.php @ 6286

Last change on this file since 6286 was 6286, checked in by dkahn, 13 years ago

#1198 Added jQuery plus plugins

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