Quantcast
Channel: DataTables 1.9 — DataTables forums
Viewing all articles
Browse latest Browse all 1816

Table ID Warning and JSON formatting error

$
0
0
Hi,
I am getting the following errors:
DataTables warning (table id = 'example'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error

The URL: http://www.njacfootball.com/test/version3/helmets/allstars/
The Debugger ID: asulet

When checking Firebug, it looks like the data is there, but just not displaying. Any help would be appreciated.


//--------------Display---------------
<style type="text/css" title="currentStyle">
			@import "http://njacfootball.com/styles/DataTables-1.9.4/media/css/demo_page.css";
			@import "http://njacfootball.com/styles/DataTables-1.9.4/media/css/demo_table.css";
		</style>
		<script type="text/javascript" language="javascript" src="http://njacfootball.com/styles/DataTables-1.9.4/media/js/jquery.js"></script>
		<script type="text/javascript" language="javascript" src="http://njacfootball.com/styles/DataTables-1.9.4/media/js/jquery.dataTables.js"></script>
		<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#example').dataTable( {
					"bJQueryUI": true,
					"bProcessing": true,
					"bServerSide": true,
					"sPaginationType": "full_numbers",
					"sAjaxSource": "teamresultsGet.php",
					"fnServerParams": function (aoData) {
						aoData.push( { "name": "yr", "value": "<? echo $yr; ?>" } );
						aoData.push( { "name": "sch", "value": "<? echo $sch; ?>" } );
						aoData.push( { "name": "team", "value": "<? echo $team; ?>" } );	
					},
					"iDisplayLength": 25,
					"aaSorting": [[ 4, "asc" ],[ 5, "asc" ],[ 0, "asc" ],[ 1, "asc" ]],
					"aoColumns": [{ "bSortable":true }, { "bSortable":true }, { "bSortable":false }, { "bSortable":true }, { "bSortable":true }, { "bSortable":true }]
				} );
			} );
		</script>

<div id="dynamic">
    	<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
    		<thead>
    		<tr>
    			<th width="">Last<br>Name</th>
                <th width="">First<br>Name</th> 
                <th width="">Grad<br>Year</th>
                <th width="">School</th>
                <th width="">Team<br>No.</th> 
                <th width="">O / D</th> 
			</tr>
    		</thead>
    		<tbody>
    			<tr>
    				<td colspan="6" class="dataTables_empty" style="font:Georgia, 'Times New Roman', Times, serif 14px;">Loading data from server</td>
    			</tr>
    		</tbody>
   			<tfoot>
            <tr>
                <th>Last<br>Name</th>
                <th>First<br>Name</th> 
                <th>Grad<br>Year</th>
                <th>School</th>
                <th>Team<br>No.</th>  
                <th>O / D</th> 
              </tr>
    		</tfoot>
    	</table>
        <br><br>
        <strong>KEY:</strong><br>
        Grad Year:  High School Graduation Year<br>
        O/D:  Offense or Defense
    </div>

//----------------------------------Get Data----

	$yr=$_GET['yr'];
	$sch=$_GET['sch'];
	$team=$_GET['team'];
	
	//'AllTeam'
		
	if ($team<>'all') {
	$aColumns = array( 'LName', 'FName', 'GradYear', 'HighSchool', 'TeamNum', 'OD' );
	//$aColumns = array( 'LName', 'FName', 'HighSchool', 'TeamNum', 'OD' );
	}
	
	/* Indexed column (used for fast and accurate table cardinality) */
	$sIndexColumn = "LName";
	
	/* DB table to use */
	$tmTable = 'player_allteam';
	$deTable = 'player_details';
	$tmidTable = 'player_allteam_tbl';
	
	/* Database connection information */
	$gaSql['user']       = "";
	$gaSql['password']   = "";
	$gaSql['db']         = "";
	$gaSql['server']     = "";
	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 * 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
	 */
	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 = "WHERE (AllTeamYear='".$yr."') AND ($tmTable.PD_ID=$deTable.PD_ID) AND ($tmidTable.Team_Name=$tmTable.AllTeam)";
	
	//WHERE School is specified
	if ($sch<>'all') {
		$sWhere .= " AND (School='".$sch."')"; }
	//WHERE All Team is specified
	if ($team<>'all') {
		$sWhere .= " AND (Team_ID='".$team."')"; }
	
	
	if ( $_GET['sSearch'] != "" )
	{
		$sWhere .= " AND (";
		for ( $i=0 ; $i<count($aColumns) ; $i++ )
		{
			$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] != '' )
		{
			$sWhere .= " AND (";
			$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ) ";
		}
	}

	 /* SQL queries
	 * Get data to display
	 */
	//DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.
	$sQuery = "
		SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
		FROM   $deTable, $tmTable, $tmidTable 
		$sWhere
		$sOrder
		$sLimit
	";
	echo $sQuery;
	$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 die(mysql_error());
	$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
	$iFilteredTotal = $aResultFilterTotal[0];
	
	/* Total data set length */
	$sQuery = "
		SELECT COUNT(`".$sIndexColumn."`)
		FROM   $deTable 
	";
	$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
	$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] == "GradYear" )
			{
				//Special output formatting for 'version' column 
				$row[] = ($aRow[ $aColumns[$i] ]=="0000") ? '-' : $aRow[ $aColumns[$i] ];
			}
			else if ( $aColumns[$i] != ' ' )
			{ 
				/* General output */
				$row[] = $aRow[ $aColumns[$i] ];
			}
		}
		$output['aaData'][] = $row;
	}
	
	echo json_encode( $output );

          mysql_close();  ?>


Viewing all articles
Browse latest Browse all 1816

Trending Articles