【发布时间】:2010-10-10 05:10:09
【问题描述】:
我正在使用 PHP 从 MYSQL 读取文本字段 (DESCRIPTION) 并准备一个 JSON 对象。以 JQUERY DataTables Plug-In 示例为蓝本。
DESCRIPTION 字段包含回车,我相信这会导致 JSON 无效。 JSONLINT.com 产生“语法错误,第 35 行出现意外的 TINVALID。解析失败”。
http://ageara.com/exp3/server_processing_details_col.php 应该返回有效的 JSON
我正在尝试使用 PHP TRIM() 函数来删除 CR,但运气不佳。
相关 PHP 代码如下 ....
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
$sQuery = "
SELECT COUNT(TITLE)
FROM geometa_small
";
$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
/* write the JSON output */
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$sOutput .= "[";
$sOutput .= '"<img src=\"details_open.png\">",';
$sOutput .= '"'.str_replace('"', '\"', $aRow['TITLE']).'",';
$sOutput .= '"'.str_replace('"', '\"', $aRow['DATA_CUSTODIAN_ORGANIZATION']).'",';
$sOutput .= '"'.str_replace('"', '\"', $aRow['RECORD_TYPE']).'",';
$sOutput .= '"'.str_replace('"', '\"', $aRow['RESOURCE_STATUS']).'",';
$sOutput .= '"'.str_replace('"', '\"', $aRow['RESOURCE_STORAGE_LOCATION']).'",';
$sOutput .= '"'.str_replace('"', '\"', $aRow['UNIQUE_METADATA_URL']).'",';
$sOutput .= '"'.trim(str_replace('"', '\"', $aRow['DESCRIPTION']), "/r").'"';
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';
echo $sOutput;
function fnColumnToField( $i )
{
/* Note that column 0 is the details column */
if ( $i == 0 ||$i == 1 )
return "TITLE";
else if ( $i == 2 )
return "DATA_CUSTODIAN_ORGANIZATION";
else if ( $i == 3 )
return "RECORD_TYPE";
else if ( $i == 4 )
return "RESOURCE_STATUS";
else if ( $i == 5 )
return "RESOURCE_STORAGE_LOCATION";
else if ( $i == 6 )
return "UNIQUE_METADATA_URL";
else if ( $i == 7 )
return "DESCRIPTION";
}
?>
【问题讨论】:
标签: php mysql json jquery-plugins