Php Datatables服务器端选择其中的行
我正在使用Datatables显示sql数据库中所有注册用户的列表。我正在使用datatables.net网站上提供的服务器端php代码,我需要做的是创建一个datatable,仅当所有用户在性别列中被列为男性时,才会显示这些用户。通常使用php,我会执行以下操作:Php Datatables服务器端选择其中的行,php,sql,datatables,jquery-datatables,Php,Sql,Datatables,Jquery Datatables,我正在使用Datatables显示sql数据库中所有注册用户的列表。我正在使用datatables.net网站上提供的服务器端php代码,我需要做的是创建一个datatable,仅当所有用户在性别列中被列为男性时,才会显示这些用户。通常使用php,我会执行以下操作: SELECT * FROM users WHERE Gender = Male 但就我个人而言,我无法解决如何使用数据表实现这一点。以下是datatables服务器端代码: /* * Script: DataTables
SELECT * FROM users WHERE Gender = Male
但就我个人而言,我无法解决如何使用数据表实现这一点。以下是datatables服务器端代码:
/*
* 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)
*/
$aColumns = array( 'FName', 'LName', 'Gender', 'Email');
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "id";
/* DB table to use */
$sTable = "users";
/* Database connection information */
$gaSql['user'] = "";
$gaSql['password'] = "";
$gaSql['db'] = "";
$gaSql['server'] = "localhost";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* Local functions
*/
function fatal_error ( $sErrorMessage = '' )
{
header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
die( $sErrorMessage );
}
/*
* MySQL connection
*/
if ( ! $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) )
{
fatal_error( 'Could not open connection to server' );
}
if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) )
{
fatal_error( 'Could not select database ' );
}
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
intval( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
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] ) ]."
".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc').", ";
}
}
$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 ( $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
{
$sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_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 fatal_error( 'MySQL Error: ' . mysql_errno() );
/* Data set length after filtering */
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$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 fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
/*
* Output
*/
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$sOutput .= "[";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
/* Special output formatting for 'version' */
$sOutput .= ($aRow[ $aColumns[$i] ]=="0") ?
'"-",' :
'"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$sOutput .= '"'.str_replace(
array( '"', "\n", "\r" ),
array( '\\"', "\\n", "\\n"),
$aRow[ $aColumns[$i] ] ).'",';
}
}
/*
* Optional Configuration:
* If you need to add any extra columns (add/edit/delete etc) to the table, that aren't in the
* database - you can do it here
*/
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';
echo $sOutput;
您可以从以下开始修改筛选部分:
$sWhere = "";
或者实际的查询创建更进一步:
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
在这两种情况下,问题是如果您在datatables中使用任何额外的筛选,然后考虑和/或构建可以破坏它的where
在我的例子中,我删除了过滤中的一行,该行在运行实际的$sQuery之前关闭where,如果$sWhere==则检查$sQuery,并根据哪个结果有两个不同的查询,一个有$sWhere,另一个没有,但都有my where,然后手动将结束括号添加到查询中
根据记忆,我使用了如下未测试的内容,因此可能需要调整:
if ($sWhere=="") {
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
Where Gender='Male'
$sOrder
$sLimit
";
} else {
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere AND Gender='Male'".)."
$sOrder
$sLimit
";
}
也许有更好的方法可以做到这一点,但它对我很有效 感谢回复,我得到了错误:DataTables警告table id='dt_table_tools':DataTables警告:无法解析来自服务器的JSON数据。这是由JSON格式错误引起的。我正在努力解决这个问题,但如果你能先解决,请让我知道。thanksIt看起来您正在使用表工具,但我一直无法让表工具与服务器端处理一起正常工作-原则上,您的查询应该是正常的,否则在应用mysql\u查询时会中断。尝试直接在浏览器中运行服务器端脚本,即不在数据表中运行,查看它是否正确显示数据或给出错误