Hello Everyone here,
I am trying to build this server side processing script based on some post I found from other users, but I cannot get the sort and filter function to work. Its been a week now, at the moment for simplicity I use just a simple table, but later I would use join function. If anyone could give me a helping hand why sorting and filtering not working, it would be great.
I am trying to build this server side processing script based on some post I found from other users, but I cannot get the sort and filter function to work. Its been a week now, at the moment for simplicity I use just a simple table, but later I would use join function. If anyone could give me a helping hand why sorting and filtering not working, it would be great.
<?php /* * Script: DataTables server-side script for PHP and MySQL * Copyright: 2010 - Allan Jardine * License: GPL v2 or BSD (3-point) */ /* * * * * * * * * * * Easy set variables * * * * * * * * */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Array of database columns which should be read and sent back to * DataTables. Use a space where you want to insert a non-database field * (for example a counter or static image) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ session_start(); if(!session_is_registered(name)){ header("location:login.php"); } define('INCLUDE_CHECK',true); include 'config.inc.php'; include 'includes/member_common.php'; $aColumns = array("s.id AS id", "s.start_date AS start_date", "s.days AS days", "s.site AS site"); /*, "a.name site", "b.name guest1", "c.name guest2", "d.name guest3", "e.name guest4"); */ /* Indexed column (used for fast and accurate table cardinality) */ $sIndexColumn = "s.id"; /* DB table to use */ $sTable = "schedules s"; // any JOIN operations that you need to do $sJoins = ""; /*"LEFT JOIN texchanges a on s.site = a.id LEFT JOIN texchanges b on s.guest1 = b.id LEFT JOIN texchanges c on s.guest2 = c.id LEFT JOIN texchanges d on s.guest3 = d.id LEFT JOIN texchanges e on s.guest4 = e.id";*/ // get or post $_METHOD = $_POST; /* * Paging * */ $sLimit = ""; if ( isset( $_METHOD['iDisplayStart'] ) && $_METHOD['iDisplayLength'] != '-1' ) { $sLimit = "LIMIT ".mysql_real_escape_string( $_METHOD['iDisplayStart'] ).", ". mysql_real_escape_string( $_METHOD['iDisplayLength'] ); } /* * Ordering */ if ( isset( $_METHOD['iSortCol_0'] ) ) { $sOrder = "ORDER BY "; for ( $i=0 ; $i<intval( $_METHOD['iSortingCols'] ) ; $i++ ) { if ( $_METHOD[ 'bSortable_'.intval($_METHOD['iSortCol_'.$i]) ] == "true" ) { $sFieldname = $aColumns[ intval( $_METHOD['iSortCol_'.$i] ) ]; if(strpos($sFieldname, " AS ") == "TRUE") { $arr = split(" ", $sFieldname); $sFieldname = $arr[2]; } $sOrder .= $sFieldname . " ".mysql_real_escape_string( $_METHOD['sSortDir_'.$i] ) .", "; } } if ( $sOrder === "ORDER BY " ) { $sOrder = ""; } } /* * Filtering * NOTE this does not match the built-in DataTables filtering which does it * word by word on any field. It's possible to do here, but concerned about efficiency * on very large tables, and MySQL's regex functionality is very limited */ $sWhere = ""; /*"WHERE a.userid = $member[id] and a.status = 'Active'";*/ if ( $_METHOD['sSearch'] != "" ) { $sWhere .= " and ("; for ( $i=0 ; $i<count($aColumns) ; $i++ ) { $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_METHOD['sSearch'] )."%' OR "; } $sWhere = substr_replace( $sWhere, "", -3 ); $sWhere .= ')'; } /* Individual column filtering */ for ( $i=0 ; $i<count($aColumns) ; $i++ ) { if ( $_METHOD['bSearchable_'.$i] == "true" && $_METHOD['sSearch_'.$i] != '' ) { if ( $sWhere == "" ) { $sWhere = "WHERE "; } else { $sWhere .= " AND "; } $sFieldname = $aColumns[$i]; if(strpos($sFieldname, " AS ") !== false) { $arr = split(" ", $sFieldname); $sFieldname = $arr[2]; } $sWhere .= $sFieldname." LIKE '%".mysql_real_escape_string($_METHOD['sSearch_'.$i])."%' "; } } /* * SQL queries * Get data to display */ $sQuery = " SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))." FROM $sTable $sJoins $sWhere $sOrder $sLimit "; $sDataQuery = $sQuery; $rResult = mysql_query($sQuery) or die(mysql_error()); /* Data set length after filtering */ $sQuery = " SELECT FOUND_ROWS() "; $rResultFilterTotal = mysql_query( $sQuery ) or die(mysql_error()); $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal); $iFilteredTotal = $aResultFilterTotal[0]; /* Total data set length */ $sQuery = " SELECT COUNT(".$sIndexColumn.") FROM $sTable $sWhere "; $rResultTotal = mysql_query( $sQuery ) or die(mysql_error()); $aResultTotal = mysql_fetch_array($rResultTotal); $iTotal = $aResultTotal[0]; /* * Output */ $output = array( "sEcho" => intval($_GET['sEcho']), "iTotalRecords" => $iTotal, "iTotalDisplayRecords" => $iFilteredTotal, "aaData" => array() ); while ($aRow = mysql_fetch_array($rResult)) { $row = array(); for ( $i=0 ; $i<count($aColumns) ; $i++ ) { if(strpos($aColumns[$i], " AS ") == true) { $arr = split(" ", $aColumns[$i]); $row[] = $aRow[$arr[2]]; } elseif ( $aColumns[$i] == "version" ) { /* Special output formatting for 'version' column */ $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ]; } elseif ( $aColumns[$i] != ' ' ) { /* General output */ $row[] = $aRow[ $aColumns[$i] ]; } } $output['aaData'][] = $row; } echo json_encode( $output ); ?>