annotate DataTables-1.9.4/examples/server_side/scripts/objects_jsonp.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 /*
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
57 * Paging
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
58 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
59 $sLimit = "";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
60 if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
61 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
62 $sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
63 intval( $_GET['iDisplayLength'] );
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 /*
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
68 * Ordering
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
69 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
70 $sOrder = "";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
71 if ( isset( $_GET['iSortCol_0'] ) )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
72 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
73 $sOrder = "ORDER BY ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
74 for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
75 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
76 if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
77 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
78 $iColumnIndex = array_search( $_GET['mDataProp_'.$_GET['iSortCol_'.$i]], $aColumns );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
79 $sOrder .= "`".$aColumns[ $iColumnIndex ]."` ".
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
80 ($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
81 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
82 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
83
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
84 $sOrder = substr_replace( $sOrder, "", -2 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
85 if ( $sOrder == "ORDER BY" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
86 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
87 $sOrder = "";
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
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
92 /*
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
93 * Filtering
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
94 * NOTE this does not match the built-in DataTables filtering which does it
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
95 * word by word on any field. It's possible to do here, but concerned about efficiency
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
96 * on very large tables, and MySQL's regex functionality is very limited
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
97 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
98 $sWhere = "";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
99 if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
100 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
101 $sWhere = "WHERE (";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
102 for ( $i=0 ; $i<count($aColumns) ; $i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
103 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
104 if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
105 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
106 $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
107 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
108 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
109 $sWhere = substr_replace( $sWhere, "", -3 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
110 $sWhere .= ')';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
111 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
112
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
113 /* Individual column filtering */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
114 for ( $i=0 ; $i<count($aColumns) ; $i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
115 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
116 if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
117 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
118 if ( $sWhere == "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
119 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
120 $sWhere = "WHERE ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
121 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
122 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
123 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
124 $sWhere .= " AND ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
125 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
126 $iColumnIndex = array_search( $_GET['mDataProp_'.$i], $aColumns );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
127 $sWhere .= $aColumns[$iColumnIndex]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
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
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
131
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
132 /*
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
133 * SQL queries
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
134 * Get data to display
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
135 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
136 $sQuery = "
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
137 SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
138 FROM $sTable
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
139 $sWhere
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
140 $sOrder
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
141 $sLimit
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
142 ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
143 $rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
144
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
145 /* Data set length after filtering */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
146 $sQuery = "
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
147 SELECT FOUND_ROWS()
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
148 ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
149 $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
150 $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
151 $iFilteredTotal = $aResultFilterTotal[0];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
152
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
153 /* Total data set length */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
154 $sQuery = "
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
155 SELECT COUNT(".$sIndexColumn.")
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
156 FROM $sTable
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
157 ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
158 $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
159 $aResultTotal = mysql_fetch_array($rResultTotal);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
160 $iTotal = $aResultTotal[0];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
161
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
162
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
163 /*
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
164 * Output
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
165 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
166 $output = array(
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
167 "sEcho" => intval($_GET['sEcho']),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
168 "iTotalRecords" => $iTotal,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
169 "iTotalDisplayRecords" => $iFilteredTotal,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
170 "aaData" => array()
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
171 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
172
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
173 while ( $aRow = mysql_fetch_array( $rResult ) )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
174 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
175 $row = array();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
176 for ( $i=0 ; $i<count($aColumns) ; $i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
177 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
178 if ( $aColumns[$i] == "version" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
179 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
180 /* Special output formatting for 'version' column */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
181 $row[ $aColumns[$i] ] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
182 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
183 else if ( $aColumns[$i] != ' ' )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
184 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
185 /* General output */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
186 $row[ $aColumns[$i] ] = $aRow[ $aColumns[$i] ];
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 $output['aaData'][] = $row;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
190 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
191
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
192 echo $_GET['callback'].'('.json_encode( $output ).');';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
193 ?>