I am trying to implement the basic json data source. It's not working.

I am trying to implement the basic json data source. It's not working.

johnemc2johnemc2 Posts: 4Questions: 2Answers: 0
edited December 2016 in Free community support

I'm trying to use DataTables for the first time ever. I've created a simple php script that pulls every row from the database and uses json_encode($result) to encode the data to json format. I'm not getting a result at all.

The script outputs to:
jonathanryangrice.com/trips/retrieve.php

The page that is suppose to show the data in a table is:
jonathanryangrice.com/trips/retrieve.html

I know there are errors. I can't seem to fix them. Any help would be appreciated.

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Your table has 8 columns, your json has 9 fields. They have to match.
    Also your HTML is invalid: table must have "tbody" tags.

  • sinanktpsinanktp Posts: 3Questions: 0Answers: 3
    edited December 2016 Answer ✓

    if the last line of your retrieve.php is like that
    echo json_encode($result);

    change to this

    echo '{"data":'.json_encode($result).',"options":[],"files":[]}';

  • kthorngrenkthorngren Posts: 22,162Questions: 26Answers: 5,102

    I think you need to put your JSON into a data object so it will look something like this:
    {data: [{id: "36",....}, {id: "37",....}]}

    Kevin

  • johnemc2johnemc2 Posts: 4Questions: 2Answers: 0
    edited December 2016

    Thanks sinanktp. It worked with your answer. However. It wouldn't work with the full echo line.
    echo '{"data":'.json_encode($result).',"options":[],"files":[]}';

    but only
    echo '{"data":'.json_encode($result).'}';

    Thanks again.

  • allanallan Posts: 64,756Questions: 1Answers: 10,715 Site admin

    echo '{"data":'.json_encode($result).',"options":[],"files":[]}';

    It really depends exactly on what is in $result. jsonlint.com is what I use to validate JSON and find any errors.

    Allan

This discussion has been closed.