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