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_post.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.8 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( $_POST['iDisplayStart'] ) && $_POST['iDisplayLength'] != '-1' )
47  {
48    $sLimit = "LIMIT ".mysql_real_escape_string( $_POST['iDisplayStart'] ).", ".
49      mysql_real_escape_string( $_POST['iDisplayLength'] );
50  }
51 
52 
53  /*
54   * Ordering
55   */
56  if ( isset( $_POST['iSortCol_0'] ) )
57  {
58    $sOrder = "ORDER BY  ";
59    for ( $i=0 ; $i<intval( $_POST['iSortingCols'] ) ; $i++ )
60    {
61      if ( $_POST[ 'bSortable_'.intval($_POST['iSortCol_'.$i]) ] == "true" )
62      {
63        $sOrder .= $aColumns[ intval( $_POST['iSortCol_'.$i] ) ]."
64          ".mysql_real_escape_string( $_POST['sSortDir_'.$i] ) .", ";
65      }
66    }
67   
68    $sOrder = substr_replace( $sOrder, "", -2 );
69    if ( $sOrder == "ORDER BY" )
70    {
71      $sOrder = "";
72    }
73  }
74 
75 
76  /*
77   * Filtering
78   * NOTE this does not match the built-in DataTables filtering which does it
79   * word by word on any field. It's possible to do here, but concerned about efficiency
80   * on very large tables, and MySQL's regex functionality is very limited
81   */
82  $sWhere = "";
83  if ( $_POST['sSearch'] != "" )
84  {
85    $sWhere = "WHERE (";
86    for ( $i=0 ; $i<count($aColumns) ; $i++ )
87    {
88      $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_POST['sSearch'] )."%' OR ";
89    }
90    $sWhere = substr_replace( $sWhere, "", -3 );
91    $sWhere .= ')';
92  }
93 
94  /* Individual column filtering */
95  for ( $i=0 ; $i<count($aColumns) ; $i++ )
96  {
97    if ( $_POST['bSearchable_'.$i] == "true" && $_POST['sSearch_'.$i] != '' )
98    {
99      if ( $sWhere == "" )
100      {
101        $sWhere = "WHERE ";
102      }
103      else
104      {
105        $sWhere .= " AND ";
106      }
107      $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_POST['sSearch_'.$i])."%' ";
108    }
109  }
110 
111 
112  /*
113   * SQL queries
114   * Get data to display
115   */
116  $sQuery = "
117    SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
118    FROM   $sTable
119    $sWhere
120    $sOrder
121    $sLimit
122  ";
123  $rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
124 
125  /* Data set length after filtering */
126  $sQuery = "
127    SELECT FOUND_ROWS()
128  ";
129  $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
130  $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
131  $iFilteredTotal = $aResultFilterTotal[0];
132 
133  /* Total data set length */
134  $sQuery = "
135    SELECT COUNT(".$sIndexColumn.")
136    FROM   $sTable
137  ";
138  $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
139  $aResultTotal = mysql_fetch_array($rResultTotal);
140  $iTotal = $aResultTotal[0];
141 
142 
143  /*
144   * Output
145   */
146  $sOutput = '{';
147  $sOutput .= '"sEcho": '.intval($_POST['sEcho']).', ';
148  $sOutput .= '"iTotalRecords": '.$iTotal.', ';
149  $sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
150  $sOutput .= '"aaData": [ ';
151  while ( $aRow = mysql_fetch_array( $rResult ) )
152  {
153    $sOutput .= "[";
154    for ( $i=0 ; $i<count($aColumns) ; $i++ )
155    {
156      if ( $aColumns[$i] == "version" )
157      {
158        /* Special output formatting for 'version' */
159        $sOutput .= ($aRow[ $aColumns[$i] ]=="0") ?
160          '"-",' :
161          '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
162      }
163      else if ( $aColumns[$i] != ' ' )
164      {
165        /* General output */
166        $sOutput .= '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
167      }
168    }
169   
170    /*
171     * Optional Configuration:
172     * If you need to add any extra columns (add/edit/delete etc) to the table, that aren't in the
173     * database - you can do it here
174     */
175   
176   
177    $sOutput = substr_replace( $sOutput, "", -1 );
178    $sOutput .= "],";
179  }
180  $sOutput = substr_replace( $sOutput, "", -1 );
181  $sOutput .= '] }';
182 
183  echo $sOutput;
184?>
Note: See TracBrowser for help on using the repository browser.