annotate DataTables-1.9.4/examples/server_side/scripts/filter_col.php @ 0:ac5f9272033b draft

first upload
author saskia-hiltemann
date Tue, 01 Jul 2014 11:42:23 -0400
parents
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 /* MySQL connection */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
3 include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" ); /* ;-) */
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 * Local functions
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
7 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
8 function fatal_error ( $sErrorMessage = '' )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
9 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
10 header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
11 die( $sErrorMessage );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
12 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
13
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
14
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
15 /*
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
16 * MySQL connection
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
17 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
18 if ( ! $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
19 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
20 fatal_error( 'Could not open connection to server' );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
21 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
22
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
23 if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
24 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
25 fatal_error( 'Could not select database ' );
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
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
29
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
30 /* Paging */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
31 $sLimit = "";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
32 if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
33 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
34 $sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
35 intval( $_GET['iDisplayLength'] );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
36 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
37
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
38 /* Ordering */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
39 if ( isset( $_GET['iSortCol_0'] ) )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
40 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
41 $sOrder = "ORDER BY ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
42 for ( $i=0 ; $i<mysql_real_escape_string( $_GET['iSortingCols'] ) ; $i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
43 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
44 $sOrder .= fnColumnToField(mysql_real_escape_string( $_GET['iSortCol_'.$i] ))."
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
45 ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
46 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
47 $sOrder = substr_replace( $sOrder, "", -2 );
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 /* Filtering - NOTE this does not match the built-in DataTables filtering which does it
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
51 * word by word on any field. It's possible to do here, but concerned about efficiency
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
52 * on very large tables, and MySQL's regex functionality is very limited
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
53 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
54 $sWhere = "";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
55 if ( $_GET['sSearch'] != "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
56 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
57 $sWhere = "WHERE ( engine LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ".
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
58 "browser LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ".
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
59 "platform LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ".
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
60 "version LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ".
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
61 "grade LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' )";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
62 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
63
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
64 for ( $i=0 ; $i<$_GET['iColumns'] ; $i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
65 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
66 if ( $_GET['sSearch_'.$i] != '' )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
67 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
68 if ( $sWhere != "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
69 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
70 $sWhere .= " AND ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
71 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
72 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
73 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
74 $sWhere .= "WHERE ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
75 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
76 $sWhere .= fnColumnToField($i) ." LIKE '%".mysql_real_escape_string( $_GET['sSearch_'.$i] )."%'";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
77 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
78 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
79
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
80 $sQuery = "
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
81 SELECT SQL_CALC_FOUND_ROWS id, engine, browser, platform, version, grade
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
82 FROM ajax
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
83 $sWhere
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
84 $sOrder
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
85 $sLimit
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
86 ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
87 $rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
88
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
89 $sQuery = "
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
90 SELECT FOUND_ROWS()
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
91 ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
92 $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
93 $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
94 $iFilteredTotal = $aResultFilterTotal[0];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
95
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
96 $sQuery = "
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
97 SELECT COUNT(id)
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
98 FROM ajax
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
99 ";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
100 $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
101 $aResultTotal = mysql_fetch_array($rResultTotal);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
102 $iTotal = $aResultTotal[0];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
103
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
104 $sOutput = '{';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
105 $sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
106 $sOutput .= '"iTotalRecords": '.$iTotal.', ';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
107 $sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
108 $sOutput .= '"aaData": [ ';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
109 while ( $aRow = mysql_fetch_array( $rResult ) )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
110 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
111 $sOutput .= "[";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
112 $sOutput .= '"'.str_replace('"', '\"', $aRow['engine']).'",';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
113 $sOutput .= '"'.str_replace('"', '\"', $aRow['browser']).'",';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
114 $sOutput .= '"'.str_replace('"', '\"', $aRow['platform']).'",';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
115 if ( $aRow['version'] == "0" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
116 $sOutput .= '"-",';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
117 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
118 $sOutput .= '"'.str_replace('"', '\"', $aRow['version']).'",';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
119 $sOutput .= '"'.str_replace('"', '\"', $aRow['grade']).'"';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
120 $sOutput .= "],";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
121 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
122 $sOutput = substr_replace( $sOutput, "", -1 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
123 $sOutput .= '] }';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
124
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
125 echo $sOutput;
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 function fnColumnToField( $i )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
129 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
130 if ( $i == 0 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
131 return "engine";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
132 else if ( $i == 1 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
133 return "browser";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
134 else if ( $i == 2 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
135 return "platform";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
136 else if ( $i == 3 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
137 return "version";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
138 else if ( $i == 4 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
139 return "grade";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
140 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
141 ?>