How to get 2D array in DataTable from json_encode($sOutString)

How to get 2D array in DataTable from json_encode($sOutString)

jewel03csejewel03cse Posts: 11Questions: 0Answers: 0
edited December 2010 in General
Hi, I have some fields in mysql database which contain value including ' , " , \n, \t etc. character. Hence I have used json_encode() function for encoding. But in the fnServerData(sSource, aoData, fnCallback){ }, how can I extract encoded value to get 2D array for using in datatable ?
Code segment in PHP:
[code]

$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_POST['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
$serial=$_POST['iDisplayStart']+1;
$x="";
$y="";

$f=0;
while ( $aRow = mysql_fetch_array( $rResult ) )
{ if($f++) $sOutput .=',';
$sOutput .= "[";
$sOutput .= '"'.addslashes($aRow['ID']).'",';
$sOutput .= '"'.$serial++.'",';
$sOutput .= '"'.addslashes($aRow['Upazila']).'",';
$sOutput .= '"'.addslashes($aRow['PAName']).'",';
$sOutput .= '"'.addslashes($aRow['UnderWH']).'",';
$sOutput .= '"'.addslashes($aRow['ComputerStatus']).'",';
$sOutput .= '"'.addslashes($aRow['UIMSoperational']).'",';
$sOutput .= '"'.addslashes($aRow['PrinterStatus']).'",';
$sOutput .= '"'.addslashes($aRow['Problem']).'",';
$sOutput .= '"'.addslashes($aRow['InternetStatus']).'",';
$sOutput .= '"'.addslashes($aRow['Remarks']).'",';
$sOutput .= '"'.$x.$y.'"';
$sOutput .= "]";
}
$sOutput .= '] }';

echo json_encode($sOutput);

[/code]

In java script
[code]

"fnServerData": function ( sSource, aoData, fnCallback )
{
aoData.push({"name":"operation", "value": "upazilaInfoListFetch"});

$.ajax({
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});/// end of $.ajax()
}

[/code]

please give code example or link ?

Replies

  • jewel03csejewel03cse Posts: 11Questions: 0Answers: 0
    Please, give the link where it is in this documentation if exist?
  • allanallan Posts: 63,508Questions: 1Answers: 10,471 Site admin
    The use of json_encode() is in error there I believe - since you are encoding a JSON string, rather than a Javascript object. If you want to use json_encode then have a look at this example: http://datatables.net/development/server-side/php_mysql . An alternative is just to remove \n and \r since they won't be rendered in the HTML anyway.

    Allan
  • jewel03csejewel03cse Posts: 11Questions: 0Answers: 0
    edited December 2010
    Thanks Allan for your response and for indicating the use of json_encode(). But, what or how should I do in DataTable to get 2D array ? Please, give link if exists.
  • allanallan Posts: 63,508Questions: 1Answers: 10,471 Site admin
    You'd use it rather like in the link I gave before... :-) That will create the 2D array which DataTables needs.

    Allan
This discussion has been closed.