0

I'm using a jquery plugin called DataTables, and it requires me to return a json string in order to render the table.

Currently, the outputted json looks like this (see the aaData array):

{"sEcho":0,"aaData":[[{"ID":"1","idPatient":"122342","idFacility":"3","idTreatment":"3"}]]}

I'm wondering if the { } braces should actually be in the aaData array. In fact, I think the braces are actually what's causing the JSON parse error.

The actual code that generates this is listed below. (core->dbh is a PDO handle)

<?php

require_once('core/Core.php');
$core = Core::get_instance();

        $sql = 'SELECT ID, idPatient, idFacility, idTreatment
                FROM Pathology WHERE idPatient = 122342';

        $stmt = $core->dbh->prepare($sql);

        $stmt->setFetchMode(PDO::FETCH_ASSOC);

        // bind parameters
        $stmt->execute();

        // prepare output for DataTables

        $data = array("sEcho" =>intval($_GET['sEcho']),
                      "aaData" =>array()
        );

        while($result = $stmt->fetchAll()) {

                $data['aaData'][] = $result;

        }

            echo json_encode($data);


?>

Could someone please tell me how can I remove the curly braces, or if the JSON is improperly formatted in another way that could be causing the parse error?

Thanks

1 Answer 1

4

The JSON is fine. You are encoding it with PHP via json_encode, that can't be the problem. My guess would be that by doing this $data['aaData'][] = $result; you are nesting twice (see the double brackets) your results and the plugin fails. Try this: $data['aaData'] = $result;

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, i think so. Result is a array of rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.