2 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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)
9 $aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );
11 /* Indexed column (used for fast and accurate table cardinality) */
17 /* Database connection information */
19 $gaSql['password'] = "";
21 $gaSql['server'] = "localhost";
23 /* REMOVE THIS LINE (it just includes my SQL connection user/pass) */
24 include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" );
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
35 function fatal_error ( $sErrorMessage = '' )
37 header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
38 die( $sErrorMessage );
45 if ( ! $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) )
47 fatal_error( 'Could not open connection to server' );
50 if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) )
52 fatal_error( 'Could not select database ' );
60 if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
62 $sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
63 intval( $_GET['iDisplayLength'] );
71 if ( isset( $_GET['iSortCol_0'] ) )
73 $sOrder = "ORDER BY ";
74 for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
76 if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
78 $iColumnIndex = array_search( $_GET['mDataProp_'.$_GET['iSortCol_'.$i]], $aColumns );
79 $sOrder .= "`".$aColumns[ $iColumnIndex ]."` ".
80 ($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
84 $sOrder = substr_replace( $sOrder, "", -2 );
85 if ( $sOrder == "ORDER BY" )
94 * NOTE this does not match the built-in DataTables filtering which does it
95 * word by word on any field. It's possible to do here, but concerned about efficiency
96 * on very large tables, and MySQL's regex functionality is very limited
99 if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
102 for ( $i=0 ; $i<count($aColumns) ; $i++ )
104 if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
106 $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
109 $sWhere = substr_replace( $sWhere, "", -3 );
113 /* Individual column filtering */
114 for ( $i=0 ; $i<count($aColumns) ; $i++ )
116 if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
126 $iColumnIndex = array_search( $_GET['mDataProp_'.$i], $aColumns );
127 $sWhere .= $aColumns[$iColumnIndex]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
134 * Get data to display
137 SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
143 $rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
145 /* Data set length after filtering */
149 $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
150 $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
151 $iFilteredTotal = $aResultFilterTotal[0];
153 /* Total data set length */
155 SELECT COUNT(".$sIndexColumn.")
158 $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
159 $aResultTotal = mysql_fetch_array($rResultTotal);
160 $iTotal = $aResultTotal[0];
167 "sEcho" => intval($_GET['sEcho']),
168 "iTotalRecords" => $iTotal,
169 "iTotalDisplayRecords" => $iFilteredTotal,
173 while ( $aRow = mysql_fetch_array( $rResult ) )
176 for ( $i=0 ; $i<count($aColumns) ; $i++ )
178 if ( $aColumns[$i] == "version" )
180 /* Special output formatting for 'version' column */
181 $row[ $aColumns[$i] ] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
183 else if ( $aColumns[$i] != ' ' )
186 $row[ $aColumns[$i] ] = $aRow[ $aColumns[$i] ];
189 $output['aaData'][] = $row;
192 echo $_GET['callback'].'('.json_encode( $output ).');';