annotate DataTables-1.9.4/examples/server_side/scripts/custom_data_property.php @ 7:0f2b740536fb draft

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