PHP json_encode($data)

PHP json_encode($data)

bbrindzabbrindza Posts: 316Questions: 73Answers: 1

Hello fellow travelers,

I have always use DataTable ssp scripts for my back end data source. However with the limitations of the ssp.class I cannot do complex SQL such as GROUP BY and SUM .

So I went with a PHP script the returns the json_encode($data) to the DataTable js .

However, I can get the data to load. I receive this error: "Uncaught TypeError: Cannot read property 'length' of undefined"

Below is my PHP array structure and DaTables AJAX


$data[] = array( "PO_OrderNumber" => $row['APPONO'], "payableNumber" => $row['APPAY#'], "invoiceNumber" => $row['APINVC'], "vendorNumber" => $row['APVEND'], "vendorName" => $row['VMRMTN'], "entryDate" => $row['APENDT'], "itemNumber1" => $row['APIT01'], "itemNumber2" => $row['APIT02'], "itemNumber3" => $row['APIT03'], "itemQuantity1" => $row['APQT01'], "totalPayableAmount" => $row['APTLPA'], "claimNumber" => $row['FDCLMN'], "claimTotal" => $row['FCTOTAL'], );
$(document).ready(function() { 

     $('#APAgingVsFreightClaimsTable').DataTable( {
            ajax: {
                url: "FreightClaims/AccountingReports/getAPAgingFreightClaimsData.php",
                dataSrc: 'data'
            },
            columns: [
                    { "data": "PO_OrderNumber" },
                    { "data": "payableNumber" },
                    { "data": "invoiceNumber" },
                    { "data": "vendorNumber" },
                    { "data": "vendorName" },
                    { "data": "entryDate" },
                    { "data": "itemNumber1" },
                    { "data": "itemNumber2" },
                    { "data": "itemNumber3" },
                    { "data": "itemQuantity1" },
                    { "data": "totalPayableAmount" },
                    { "data": "claimNumber" },
                    { "data": "claimTotal" }
             
                ]
        } );

} );//END $(document).ready(function() 

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,328Questions: 26Answers: 4,949

    I'm not familiar with PHP so not sure of the structure returned. Please post the JSON response from the browser's network inspector tool.

    Kevin

  • bbrindzabbrindza Posts: 316Questions: 73Answers: 1

    Sample of JSON response ..

  • kthorngrenkthorngren Posts: 21,328Questions: 26Answers: 4,949
    Answer ✓

    You have dataSrc: 'data' but the JSON is not in a data object. You can either put the data in the data object or change the ajax.dataSrc option to be dataSrc: ''.

    Kevin

  • bbrindzabbrindza Posts: 316Questions: 73Answers: 1

    That worked. Thank you Kevin. First time I used this technique.

This discussion has been closed.