Hi there!
I'm am using the Server side (PHP) script to help me load the data faster when the page loads. Much like the example here: http://datatables.net/release-datatables/examples/data_sources/server_side.html
I have it working for the most part, however, I cannot figure out how to convert a timestamp from one of my columns in the database to just a date (such as MM/DD/YYYY) or the time (HH:MM am/pm). If I don't apply a date formatter, then I get the exact same timestamp in two columns; which prevents me from having the option to sort the table by time or date. I tried replacing one of my columns with a mysql DATE_FORMAT function to apply the date or time, but then I got an error that says
If you have any ideas on how I can apply the DATE_FORMAT on one of my columns from the DataBase, then please do share. Otherwise, I cannot move forward and I will have to rely on javascript to load most of the data.
Below is my "server_side_process.php" script:
Thank you for you time.
I'm am using the Server side (PHP) script to help me load the data faster when the page loads. Much like the example here: http://datatables.net/release-datatables/examples/data_sources/server_side.html
I have it working for the most part, however, I cannot figure out how to convert a timestamp from one of my columns in the database to just a date (such as MM/DD/YYYY) or the time (HH:MM am/pm). If I don't apply a date formatter, then I get the exact same timestamp in two columns; which prevents me from having the option to sort the table by time or date. I tried replacing one of my columns with a mysql DATE_FORMAT function to apply the date or time, but then I got an error that says
"Unknown column 'DATE_FORMAT(timeStamp,'%b %e, %Y')' in 'field list'
If you have any ideas on how I can apply the DATE_FORMAT on one of my columns from the DataBase, then please do share. Otherwise, I cannot move forward and I will have to rely on javascript to load most of the data.
Below is my "server_side_process.php" script:
<?php include 'functions.php'; $customerID = 65; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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) */ $aColumns = array( "DATE_FORMAT(timeStamp,'%b %e, %Y')", "DATE_FORMAT(timeStamp,'%h:%i %p')", 'identifier', 'identifier', 'viewed_Type' ); /* Indexed column (used for fast and accurate table cardinality) */ $sIndexColumn = "--id--"; /* DB table to use */ $sTable = "--table--"; /* Database connection information */ $gaSql['user'] = "--USER--"; $gaSql['password'] = "--password--"; $gaSql['db'] = "--db--"; $gaSql['server'] = "--server--"; /* REMOVE THIS LINE (it just includes my SQL connection user/pass) */ //include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" ); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * If you just want to use the basic configuration for DataTables with PHP server-side, there is * no need to edit below this line */ /* * MySQL connection */ $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or die( 'Could not open connection to server' ); mysql_select_db( $gaSql['db'], $gaSql['link'] ) or die( 'Could not select database '. $gaSql['db'] ); /* * Paging */ $sLimit = ""; if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' ) { $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ". mysql_real_escape_string( $_GET['iDisplayLength'] ); } /* * Ordering */ $sOrder = ""; if ( isset( $_GET['iSortCol_0'] ) ) { $sOrder = "ORDER BY "; for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ ) { if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" ) { $sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ". mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", "; } } $sOrder = substr_replace( $sOrder, "", -2 ); 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 = ""; if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" ) { $sWhere = "WHERE ("; for ( $i=0 ; $i<count($aColumns) ; $i++ ) { $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR "; } $sWhere = substr_replace( $sWhere, "", -3 ); $sWhere .= "AND customerIdentifier = '" . $customerID . "' AND (viewed_Type = 'i' or viewed_Type = 'ir' or viewed_Type = 'y' or viewed_Type = 'iry'))"; } else { $sWhere .= "WHERE customerIdentifier = '" . $customerID . "' AND (viewed_Type = 'i' or viewed_Type = 'ir' or viewed_Type = 'y' or viewed_Type = 'iry') "; } /* Individual column filtering */ for ( $i=0 ; $i<count($aColumns) ; $i++ ) { if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' ) { if ( $sWhere == "" ) { $sWhere = "WHERE "; } else { $sWhere .= " AND "; } $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' "; } } /* * SQL queries * Get data to display */ $sQuery = " SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."` FROM $sTable $sWhere $sOrder $sLimit "; $rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error()); /* Data set length after filtering */ $sQuery = " SELECT FOUND_ROWS() "; $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error()); $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal); $iFilteredTotal = $aResultFilterTotal[0]; /* Total data set length */ $sQuery = " SELECT COUNT(`".$sIndexColumn."`) FROM $sTable "; $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) 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 ( $aColumns[$i] == "version" ) { /* Special output formatting for 'version' column */ $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ]; } else if ( $aColumns[$i] == "identifier" ) { // GrabsMoreInformation to show $row[] = ($i==2) ? moreInformation($aRow[$aColumns[$i]]) : $aRow[$aColumns[$i]]; } else if ( $aColumns[$i] == "viewed_Type" ) { /* Show Icons instead of the letter... */ if ($aRow[$aColumns[$i]] == 'y' || $aRow[$aColumns[$i]] == 'iry') { $row[] = '<img src="../images/non-view.png" title="" style="height:30px;" />'; } else if ($aRow[$aColumns[$i]] == 'i' or $aRow[$aColumns[$i]] == 'ir') { $row[] = '<img src="../images/viewed.png" title="" style="height:30px;" />'; } else { $row[] = '<img src="../img/icons/alert.png" />'; } //$row[] = $aRow[$aColumns[$i]]; } else if ( $aColumns[$i] != ' ' ) { /* General output */ $row[] = $aRow[ $aColumns[$i] ]; } } $output['aaData'][] = $row; } echo json_encode( $output ); ?>
Thank you for you time.