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.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: 4.4 KB
RevLine 
[6286]1<?php
2  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3   * Easy set variables
4   */
5 
6  /* Array of database columns which should be read and sent back to DataTables. Use a space where
7   * you want to insert a non-database field (for example a counter or static image)
8   */
9  $aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );
10 
11  /* Indexed column (used for fast and accurate table cardinality) */
12  $sIndexColumn = "id";
13 
14  /* DB table to use */
15  $sTable = "ajax";
16 
17  /* Database connection information */
18  $gaSql['user']       = "";
19  $gaSql['password']   = "";
20  $gaSql['db']         = "";
21  $gaSql['server']     = "localhost";
22 
23  /* REMOVE THIS LINE (it just includes my SQL connection user/pass) */
24  include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" );
25 
26 
27  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
28   * If you just want to use the basic configuration for DataTables with PHP server-side, there is
29   * no need to edit below this line
30   */
31 
32  /*
33   * MySQL connection
34   */
35  $gaSql['link'] =  mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password']  ) or
36    die( 'Could not open connection to server' );
37 
38  mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
39    die( 'Could not select database '. $gaSql['db'] );
40 
41 
42  /*
43   * Paging
44   */
45  $sLimit = "";
46  if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
47  {
48    $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
49      mysql_real_escape_string( $_GET['iDisplayLength'] );
50  }
51 
52 
53  /*
54   * Ordering
55   */
56  $sOrder = "";
57  if ( isset( $_GET['iSortCol_0'] ) )
58  {
59    $sOrder = "ORDER BY  ";
60    for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
61    {
62      if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
63      {
64        $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
65          ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
66      }
67    }
68   
69    $sOrder = substr_replace( $sOrder, "", -2 );
70    if ( $sOrder == "ORDER BY" )
71    {
72      $sOrder = "";
73    }
74  }
75 
76 
77  /*
78   * Filtering
79   * NOTE this does not match the built-in DataTables filtering which does it
80   * word by word on any field. It's possible to do here, but concerned about efficiency
81   * on very large tables, and MySQL's regex functionality is very limited
82   */
83  $sWhere = "";
84  if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
85  {
86    $sWhere = "WHERE (";
87    for ( $i=0 ; $i<count($aColumns) ; $i++ )
88    {
89      $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
90    }
91    $sWhere = substr_replace( $sWhere, "", -3 );
92    $sWhere .= ')';
93  }
94 
95  /* Individual column filtering */
96  for ( $i=0 ; $i<count($aColumns) ; $i++ )
97  {
98    if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
99    {
100      if ( $sWhere == "" )
101      {
102        $sWhere = "WHERE ";
103      }
104      else
105      {
106        $sWhere .= " AND ";
107      }
108      $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
109    }
110  }
111 
112 
113  /*
114   * SQL queries
115   * Get data to display
116   */
117  $sQuery = "
118    SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
119    FROM   $sTable
120    $sWhere
121    $sOrder
122    $sLimit
123  ";
124  $rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
125 
126  /* Data set length after filtering */
127  $sQuery = "
128    SELECT FOUND_ROWS()
129  ";
130  $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
131  $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
132  $iFilteredTotal = $aResultFilterTotal[0];
133 
134  /* Total data set length */
135  $sQuery = "
136    SELECT COUNT(".$sIndexColumn.")
137    FROM   $sTable
138  ";
139  $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
140  $aResultTotal = mysql_fetch_array($rResultTotal);
141  $iTotal = $aResultTotal[0];
142 
143 
144  /*
145   * Output
146   */
147  $output = array(
148    "sEcho" => intval($_GET['sEcho']),
149    "iTotalRecords" => $iTotal,
150    "iTotalDisplayRecords" => $iFilteredTotal,
151    "aaData" => array()
152  );
153 
154  while ( $aRow = mysql_fetch_array( $rResult ) )
155  {
156    $row = array();
157    for ( $i=0 ; $i<count($aColumns) ; $i++ )
158    {
159      if ( $aColumns[$i] == "version" )
160      {
161        /* Special output formatting for 'version' column */
162        $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
163      }
164      else if ( $aColumns[$i] != ' ' )
165      {
166        /* General output */
167        $row[] = $aRow[ $aColumns[$i] ];
168      }
169    }
170    $output['aaData'][] = $row;
171  }
172 
173  echo json_encode( $output );
174?>
Note: See TracBrowser for help on using the repository browser.