JSON output isn't outputting correct information

JSON output isn't outputting correct information

srisontisrisonti Posts: 3Questions: 0Answers: 0
edited April 2012 in DataTables 1.9
Hello all,

I've been using DataTables for some time now and it's been incredibly helpful (thanks for the great work Allan!). For the most part, I've been able to debug any problems I come across by reading forums and fiddling with the code. However, I've recently come across an error related to JSON that's completely stumped me (I just posted this on StackOverflow as well: http://stackoverflow.com/questions/10180244/datatables-jquery-plugin-not-working-with-json-output).

TL;DR: There are two JSON outputs (given below) that are encoded from a PHP array. The Data Tables plugin outputs the info in the first JSON exactly as the variables are in the JSON. For the second JSON, the plugin replaces the "57" with a 0, and takes "Copy Resources(all)" and replaces it with an empty string. These are values calculated in PHP and replaced in the array before it is encoded.

NOTE: All JSON output included in this question was validated as proper JSON using JSONlint.com.

----------------------------------------------------------------------------------------------------------

I'm using the Data Tables jQuery plugin for a project at work. For the "sAjaxSource", I'm referencing a PHP file that echo's a JSON_encode on an array. Here is the JSON output with no problems:

[code]
{
"sEcho": 0,
"iTotalRecords": "1",
"iTotalDisplayRecords": "1",
"aaData": [
[
"",
"ST-STRING_564687548646541_STR_STR_STRING",
"STR",
"STRN",
"51",
"",
"",
"",
"",
"STR-STRNG",
"",
"13:12:48",
"2012-04-16",
"13:13:18",
"2012-04-16",
"str51848.654894st.str",
"0",
"ST-STRNG",
"1050739",
"DAILY"
]
]
}
[/code]

The JSON above outputs all the data exactly as shown and there's no issue with the generated table. The problem arises when I try to modify the output array before I encode it into JSON. Here is my PHP code:

[code]
$total = intval($row[18]);
$status = intval($row[4]);
$tSteps = 0;
$sSteps = 0;
$curStep = '';
$wCount = 0;
$perc = 100;

for ($i = 0; $i < sizeof($aCode); ++$i) {
if ($total >= $aCode[$i]) {
$total = $total - $aCode[$i];
$tSteps++;
}
if ($status >= $aCode[$i]) {
if ($curStep == '') {
$curStep = $aStat[$i];
}
$status = $status - $aCode[$i];
$sSteps++;
}
$wCount++;
}

$perc = round(($sSteps/$tSteps)*100);

$row[18] = (string)$curStep;
$row[4] = (string)$perc;
[/code]

The array $aCode has a series of numbers listed from largest to smallest and the $aStat is an array of strings. They are both the same length. (This shouldn't be related to the problem - output from the loop is correct.)

I get all my data from my database and put it into a 2-D array, in which $row represents one entry. There are two numbers (as can be seen above in the JSON output): 51 and 1050739. Currently, each step in $aStat is associated with a code in $aCode. The numbers in my first JSON output represent the sum of these codes. 51, for example, would be the sum of steps 1, 2, 5, and 6, whereas 1050739 would be the sum of steps 1, 2, 5, 6, 7, 12, and 21. (The code is simply 2^(step number - 1).)

After I add my calculations to the end of the PHP file, right before the array is encoded, this is the new JSON I get:

[code]
{
"sEcho": 0,
"iTotalRecords": "1",
"iTotalDisplayRecords": "1",
"aaData": [
[
"",
"ST-STRING_564687548646541_STR_STR_STRING",
"STR",
"STRN",
"57",
"",
"",
"",
"",
"STR-STRNG",
"",
"13:12:48",
"2012-04-16",
"13:13:18",
"2012-04-16",
"str51848.654894st.str",
"0",
"ST-STRNG",
"Copy Resources(all)",
"DAILY"
]
]
}
[/code]

The 57 just tells us that the project is 57% percent complete and I replaced the 1050739 with the current step. I made sure to make both the variables strings (the 57 doesn't have the % sign) before I inserted them back into the array (double checked through var_dump). I also checked my JSON in all three browsers and the javascript is not throwing any errors.

When I use the second JSON, Data Tables prints out all the variables as it normally would, but it simply replaces the 57 with a 0 and "Copy Resources(all)" with an empty string. If I remove my calculations at the end of the PHP file, the table prints out all the information just fine (doesn't turn the 51 into a 0 or anything). I also tried array_unshift and array_push to add the data to the array (in case that was the problem for some weird reason), but that didn't help either. I can't figure out why the first JSON outputs everything properly, but the second JSON replaces those two variables and then outputs everything.

Replies

  • allanallan Posts: 61,692Questions: 1Answers: 10,101 Site admin
    Can you run your table through the debugger ( http://debug.datatables.net ) so I can see what is going on please?

    Allan
  • srisontisrisonti Posts: 3Questions: 0Answers: 0
    Sorry about the delay, here's the 6-letter code: uhusow.
  • allanallan Posts: 61,692Questions: 1Answers: 10,101 Site admin
    Thanks for that - I don't actually see NULL anywhere in the debug trace. There is one row in the table, and the empty cells are just empty strings. Am I missing something?

    Allan
  • srisontisrisonti Posts: 3Questions: 0Answers: 0
    That's the same problem I'm having, actually. There aren't any null values and the empty cells are empty strings, but the JSON data that is outputted is not the data that the table shows. At the very end of the PHP file, I added the PHP code (shown above) to modify two values in the output array. I then encode the modified array with JSON_encode and nothing else was changed. However, when I do this, something is changing my values between the JSON and the actual table generated on the page. Could Javascript do that? Using var_dump, I made sure all of the variables going into the JSON_encode were string, I double and triple checked that the JSON output looked the same across all three browsers, and I verified that it was accurate JSON.

    The weird thing is, if I remove the PHP code that modifies the two values in the array, the JSON output and the data shown in the table match.

    Short example (I make every variable a string using strval before putting it back into the output array):

    Without PHP snippet modifying two values in array, the output array would read something like [2, 51, 1070539]. The JSON output would read [2, 51, 1070539], and the table would show 2, 51, and 1070539 across three columns.

    With the PHP snippet, the original array would read [2, 51, 1070539]. After being modified, it would read [2, 57, Copy Resources (all)]. The JSON output would read [2, 57, Copy Resources (all)], and the table would show 2, 0, and (nothing) across three columns.

    I'm very confused as to why this is happening.
This discussion has been closed.