JSON data from server could not be parsed. This is caused by a JSON formatting error.

JSON data from server could not be parsed. This is caused by a JSON formatting error.

cosyocchcosyocch Posts: 1Questions: 1Answers: 0
edited May 2018 in Free community support
<?php session_start(); ?>
<?php include("../include/config.php"); ?>
<?php include("../database/account.php"); ?>
<?php include("../include/lang.php"); ?> 

<?php

$columns = array
(
    "u.id",
    "u.user_no",
    "u.id",
    "d.abb",
    "us.user_type_name",
    "u.date",
    "u.a_status",
    
    "u.firstname",
    "u.middlename",
    "u.lastname"
);
 
$table = "tbl_user u";
 
$joins = "

LEFT JOIN tbl_department d ON u.department_id = d.id
 
JOIN tbl_user_type us ON u.user_type_id = us.id
 
";
 
// filtering
$sql_where = "";
if ($_GET['sSearch'] != "")
{
    $sql_where = "WHERE ";
    foreach ($columns as $column)
    {
        $sql_where .= $column . " LIKE '%" . mysql_real_escape_string( $_GET['sSearch'] ) . "%' OR ";
    }
    $sql_where = substr($sql_where, 0, -3);
}
 
// ordering
$sql_order = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
    $sql_order = "ORDER BY  ";
    for ( $i = 0; $i < mysql_real_escape_string( $_GET['iSortingCols'] ); $i++ )
    {
        $sql_order .= $columns[$_GET['iSortCol_' . $i]] . " " . mysql_real_escape_string( $_GET['sSortDir_' . $i] ) . ", ";
    }
    $sql_order = substr_replace( $sql_order, "", -2 );
}
 
// paging
$sql_limit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
    $sql_limit = "LIMIT " . mysql_real_escape_string( $_GET['iDisplayStart'] ) . ", " . mysql_real_escape_string( $_GET['iDisplayLength'] );
}
 
$main_query = mysql_query("SELECT  SQL_CALC_FOUND_ROWS " . implode(", ", $columns) . " FROM {$table} {$joins} {$sql_where }{$sql_order} {$sql_limit}")
    or die(mysql_error());
 
// get the number of filtered rows
$filtered_rows_query = mysql_query("SELECT FOUND_ROWS()")
    or die(mysql_error());
$row = mysql_fetch_array($filtered_rows_query);
$response['iTotalDisplayRecords'] = $row[0];
 
// get the number of rows in total
$total_query = mysql_query("SELECT COUNT(id) FROM {$table}")
    or die(mysql_error());
$row = mysql_fetch_array($total_query);
$response['iTotalRecords'] = $row[0];
 
// send back the number requested
$response['sEcho'] = intval($_GET['sEcho']);
 
$response['aaData'] = array();
 
// finish getting rows from the main query
while ($row = mysql_fetch_row($main_query))
{
    
$row8_sub = substr($row[8],0,1);
$row[2]="$row[7] $row8_sub. $row[9]";

if($row[6]==1){
$row[6]="<a href='#' class='tablectrl_small bGreen tipS' title='Options'><i class='fa fa-check'></i> $lang[Active]</a>";    
}
else
{
$row[6]="<a href='#' class='tablectrl_small bRed tipS' title='Options'><i class='fa fa-times'></i> $lang[Inactive]</a>";      
}

    $response['aaData'][] = $row;
        
}

echo json_encode($response);
 
?

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It would be easier to see the actual data being sent to DataTables, rather than the code that generated it...

    Cheers,

    Colin

This discussion has been closed.