Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82108

DataTables server-side processing:DataTable warning:JSON data from server could not be parsed.

$
0
0
This is my table in table.html
<table class="table responsive-table" id="sorting-advanced">

				<thead>
					<tr>
						<th scope="col">Contact_Name</th>
						<th scope="col" width="15%" class="align-center hide-on-mobile">Job_Title</th>
						<th scope="col" width="15%" class="align-center hide-on-mobile-portrait">Email</th>
						<th scope="col" width="15%" class="hide-on-tablet">Phone</th>
						
					</tr>
				</thead>

				<tfoot>
					<tr>
						<td colspan="6">
							6 entries found
						</td>
					</tr>
				</tfoot>

				<tbody>
					<tr>
						
						<td></td>
						<td></td>
						<td></td>
						<td></td>
						
					</tr>
					<tr>
						
						<td></td>
						<td></td>
						<td></td>
						<td></td>
					</tr>
					<tr>
						
						<td></td>
						<td></td>
						<td></td>
						<td></td>
					</tr>
					<tr>
						
						<td></td>
						<td></td>
						<td></td>
						<td></td>
					</tr>
					
				</tbody>

			</table>

This is the javascript on the same table.html page

<script>
 $(document).ready(function () {
            $('#sorting-advanced').dataTable({
                "bProcessing": true,
                "bServerSide": true,
                "sAjaxSource": "../js/libs/DataTables/datatable.php"
            });
        });
    </script>



And this is the datatable.php

<?php

$aColumns = array( 'Contact_Name', 'Job_Title', 'Email', 'Phone');

/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "Contact_ID";

/* DB table to use */
$sTable = "mytable";

/* Database connection information */
$gaSql['user']       = "*********";
$gaSql['password']   = "*********";
$gaSql['db']         = "*********";
$gaSql['server']     = "*********.com";

/* 
 * MySQL connection
 */
$gaSql['link'] =  mssql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password']  ) or
    die( 'Could not open connection to server' );

mssql_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 ".$_GET['iDisplayStart'] .", ".
        $_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] .", ";
        }
    }
    
    $sOrder = substr_replace( $sOrder, "", -2 );
    if ( $sOrder == "ORDER BY" )
    {
        $sOrder = "";
    }
}


/* 
 * Filtering
 
 */
$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
    $sWhere = "WHERE (";
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        $sWhere .= $aColumns[$i]." LIKE '%".$_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 '%".$_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 = mssql_query( $sQuery, $gaSql['link'] ) or die(mssql_error());

/* Data set length after filtering */
$sQuery = "
		SELECT FOUND_ROWS()
	";
$rResultFilterTotal = mssql_query( $sQuery, $gaSql['link'] ) or die(mssql_error());
$aResultFilterTotal = mssql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];

/* Total data set length */
$sQuery = "
		SELECT COUNT(".$sIndexColumn.")
		FROM   $sTable
	";
$rResultTotal = mssql_query( $sQuery, $gaSql['link'] ) or die(mssql_error());
$aResultTotal = mssql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];


/*
 * Output
 */
$output = array(
    "sEcho" => intval($_GET['sEcho']),
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iFilteredTotal,
    "aaData" => array()
);

while ( $aRow = mssql_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] != ' ' )
        {
            /* General output */
            $row[] = $aRow[ $aColumns[$i] ];
        }
    }
    $output['aaData'][] = $row;
}

echo json_encode( $output );
?>


But when i run the code it is showing error as


DataTable warning(table id='sorting-advanced');
DataTable warning:JSON data from server could not be parsed.This is caused by a JSON formating error.


Can any one please let me know why is it showing the error. Or is there something am missing. Am very new to JSON. Please help me in sorting the issue.

Viewing all articles
Browse latest Browse all 82108

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>