Including and sorting a non-database field in serverside processing
Including and sorting a non-database field in serverside processing
After much phaffing about and reading various previous posts, I found a remarkably simple solution to my problem which was "how do I sort on a calculated field which I had included in server-side processing?".
Using the sample MySQL server script, I amended the columns as so:
[code]<?php
/*
* Script: DataTables server-side script for PHP and MySQL
* Copyright: 2010 - Allan Jardine
* License: GPL v2 or BSD (3-point)
*/
/* 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)
*/
$plyrno = $_GET['playerno'];
$sColumns = array( 'ahID', 'ahseasonID', 'ahT', 'ah1stXV', 'ah2ndXV', 'ah3rdXV', 'ah4thXV', 'ah5thXV', 'ahOthXV', 'jnrapps', 'ahplayerID', '(ah1stXV + ah2ndXV + ah3rdXV + ah4thXV + ah5thXV + ahOthXV) as sumapp' );
$aColumns = array( 'ahID', 'ahseasonID', 'ahT', 'ah1stXV', 'ah2ndXV', 'ah3rdXV', 'ah4thXV', 'ah5thXV', 'ahOthXV', 'jnrapps', 'ahplayerID', 'sumapp' );
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "ahID";
/* DB table to use */
$sTable = "apphistory";
$sTable = $sTable . ' WHERE (' . $sTable . '.ahplayerID = ' . $plyrno . ')';
include('include/conn.php'); [/code]
Note the duplication and subtle amendment of $acolumns, which I cloned as $sColumns for the later MySQL SELECT statement:
[code] /*
* SQL queries
* Get data to display
*/
// changed $aColumns to $sColumns below here ********************************************
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $sColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
"; [/code]
Hope this helps someone in the future.
Pete.
Using the sample MySQL server script, I amended the columns as so:
[code]<?php
/*
* Script: DataTables server-side script for PHP and MySQL
* Copyright: 2010 - Allan Jardine
* License: GPL v2 or BSD (3-point)
*/
/* 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)
*/
$plyrno = $_GET['playerno'];
$sColumns = array( 'ahID', 'ahseasonID', 'ahT', 'ah1stXV', 'ah2ndXV', 'ah3rdXV', 'ah4thXV', 'ah5thXV', 'ahOthXV', 'jnrapps', 'ahplayerID', '(ah1stXV + ah2ndXV + ah3rdXV + ah4thXV + ah5thXV + ahOthXV) as sumapp' );
$aColumns = array( 'ahID', 'ahseasonID', 'ahT', 'ah1stXV', 'ah2ndXV', 'ah3rdXV', 'ah4thXV', 'ah5thXV', 'ahOthXV', 'jnrapps', 'ahplayerID', 'sumapp' );
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "ahID";
/* DB table to use */
$sTable = "apphistory";
$sTable = $sTable . ' WHERE (' . $sTable . '.ahplayerID = ' . $plyrno . ')';
include('include/conn.php'); [/code]
Note the duplication and subtle amendment of $acolumns, which I cloned as $sColumns for the later MySQL SELECT statement:
[code] /*
* SQL queries
* Get data to display
*/
// changed $aColumns to $sColumns below here ********************************************
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $sColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
"; [/code]
Hope this helps someone in the future.
Pete.
This discussion has been closed.
Replies
I forgot to credit user 'Patricia' for pointing me in the right direction with her previous post - it just took me a long time to find it.