annotate DataTables-1.9.4/examples/server_side/scripts/server_processing.php @ 9:7300ed4c1481 draft default tip

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