JSON data error

JSON data error

Blakea105Blakea105 Posts: 2Questions: 1Answers: 1
edited February 2016 in Free community support

so i am getting a invalid json response from the following, i cant seem to find the issue....

from the document loading:

var table = $('#table_id').DataTable({
            scrollY: 500,
            paging: false,
            select: {
                style: 'single'
            },
            "sAjaxDataProp": "",
            "ajax": "php/admin/loadOverview.php",
            "aoColumns":[
                {"mData": "Date"},
                {"mData": "JobNumber"},
                {"mData": "TotalSpend"},
                {"mData": "Product"},
                {"mData": "Vendor"},
                {"mData": "PaymentMethod"},
                {"mData": "NetSale"},
                {"mData": "Profit"},
                {"mData": "RIO"}
            ]
    });

the table itself:

<div class="tableContainer">
                    <table id="table_id" class="display" style="table-layout:fixed" >
                    <thead>
                    <tr>
                    <th>Date</th>
                    <th>Job Number</th>
                    <th>Total Spend</th>
                    <th>Product</th>
                    <th>Vendor</th>
                    <th>Payment Method</th>
                    
                    <th>Net Sale</th>
                    <th>Profit</th>
                    <th>ROI</th>
                    <!--<th>Transfer Date</th>-->
                    </tr>
                    </thead>
                    <tbody>
                        <span id="tableContent"></span>
                    </tbody>
                    </table>
                </div>

the json response script:

<?
include '../connect.php';

$json_response = array();

$purchases = mysql_query("SELECT * FROM `Purchasing`");
if(mysql_num_rows($purchases) > 0 ){
    while($purch=mysql_fetch_array($purchases)){
        $row_array['Date'] = $purch['PurchaseDate'];
        $row_array['JobNumber'] = $purch['JobNumber'];
        
        $expense_qur = "SELECT * FROM `Expenses` WHERE `JobNumber`='".$purch['JobNumber']."' ORDER BY `id` ";
        $expense_res = mysql_query($expense_qur);
        $TotalExpenses = 0;
        if(mysql_num_rows($expense_res) > 0){
            while($exp = mysql_fetch_array($expense_res)){
                $TotalExpenses += $exp['Cost'];
            }
        }
        
        $row_array['TotalSpend'] = strval($purch['PaymentAmount']+$TotalExpenses);
        
        $row_array['Product'] = $purch['Product'];
        
        
        $vendor_res = mysql_query("SELECT `Name` FROM `Contacts` WHERE `id` = '".$purch['Vendor']."'");
        $vendor_row = mysql_fetch_row($vendor_res);
        $row_array['Vendor'] = $vendor_row[0];
        
        $row_array['PaymentMethod'] = $purch['PaymentMethod'];
        
        
        $netSale = 0;
        $sale_res = mysql_query("SELECT * FROM `SalesData`");
        if(mysql_num_rows($sale_res) > 0){
            while($sale = mysql_fetch_array($sale_res)){
                $itemId = $sale['ItemId'];
                $itemInfo_qur = "SELECT * FROM `Inventory` WHERE `id` = '$itemId' ";
                $itemInfo_res = mysql_query($itemInfo_qur);
                $item = mysql_fetch_array($itemInfo_res);
                
                if($item['JobNumber'] == $purch['JobNumber']){
                    $netSale += $sale['Price']*$sale['Qty'];    
                }
            }
        }
        $row_array['NetSale'] = strval($netSale);
        
        $row_array['Profit'] =  strval($row_array['NetSale'] - $row_array['TotalSpend']);
        
        $row_array['RIO'] = strval(( $netSale/$row_array['TotalSpend']) - 1);
        
        //$row_array['TransferDate'] = strval("00/00/0000");
        
        array_push($json_response,$row_array);
        
    }
    echo json_encode($json_response);
}


<?php
>
```
?>



Formatted JSON Data ( it says its a valid response on JSON checker sites)

[
{
"Date":"0001-01-01",
"JobNumber":"0001",
"TotalSpend":"0",
"Product":"Job 0",
"Vendor":"CXTEC",
"PaymentMethod":"Other",
"NetSale":"1079",
"Profit":"1079",
"RIO":"-1"
},
{
"Date":"2016-01-28",
"JobNumber":"1001",
"TotalSpend":"8421",
"Product":"Cisco Switches, phones and servers",
"Vendor":"Gordon and Rees ",
"PaymentMethod":"Check",
"NetSale":"5501",
"Profit":"-2920",
"RIO":"-0.346752167201"
}
]
```
appreciate all the help i can get

This question has an accepted answers - jump to answer

Answers

  • Blakea105Blakea105 Posts: 2Questions: 1Answers: 1
    edited February 2016 Answer ✓

    SOLVED IT! THANKS ANYWAYS!
    the response including a PHP warning!

    Warning: Division by zero in php/admin/loadOverview.php on line 51
    [{"Date":"0001-01-01","JobNumber":"0001","TotalSpend":"0","Product":"Job 0","Vendor":"CXTEC","PaymentMethod":"Other","NetSale":"1079","Profit":"1079","RIO":"-1"},{"Date":"2016-01-28","JobNumber":"1001","TotalSpend":"8421","Product":"Cisco Switches, phones and servers","Vendor":"Gordon and Rees ","PaymentMethod":"Check","NetSale":"5501","Profit":"-2920","RIO":"-0.346752167201"}]

This discussion has been closed.