How to load data using PHP?

How to load data using PHP?

spartan2276spartan2276 Posts: 5Questions: 2Answers: 0
edited July 2017 in Free community support

**I have created the php file just like the example; however when I try to bring back data it keeps saying that "DataTables warning: table id=estimates - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1"

In the example there is a require( 'ssp.class.php' ); but I can't locate this in the downloaded package.

*** I found the ssp.class.php ****

I have also verified my json output and is valid please see the attached screenshots.

Can anyone help me solve this issue?**
HTML/Javscript:

<script> $(document).ready(function() {
$('#estimates').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "phpcom/db/dtestimates.php",
"columns": [
{ "data": "first_name" },
{ "data": "last_name" },
{ "data": "reference_num" },
{ "data": "estimate_date" },
{ "data": "expiry_date" },
{ "data": "hours" }
]
} );
} ); </script>
```<?php if ($estimate == 1) {
//Display Datagrid
echo "<h1><i class='material-icons md-36'>receipt</i>Estimates</h1>";
//echo "<div id='jsGrid'></div>";
echo "
<table id='estimates' class='display' width='100%' cellspacing='0' cellpadding='3'>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Ref. Number</th>
<th>Estimate Date</th>
<th>Expire Date</th>
<th>Hours</th>
</tr>
</thead>
</table>";
} else {
echo $est_msg;
}

<?php >``` ?>

PHP script in dtestimates.php:

<?php include 'includes/params/def_params.php'; ?>
<?php include 'includes/params/db_params.php'; ?>
<?php include 'phpcom/db/estimatesCom.php'; ?>

<?php $table = 'estimates'; $primaryKey = 'estimateid'; $columns = array( array( 'db' => 'first_name', dt => 'first_name' ), array( 'db' => 'last_name', dt => 'last_name' ), array( 'db' => 'reference_num', dt => 'reference_num' ), array( 'db' => 'estimate_date', dt => 'estimate_date', 'formatter' => function($d, $row) { return date( 'js M y', strtotime($cd)); } ), array( 'db' => 'expiry_date', dt => 'expiry_date' 'formatter' => function($d, $row) { return date( 'js M y', strtotime($cd)); } ), array( 'db' => 'hours', dt => 'hours' 'formatter' => function($d, $row) { return '$'.number_format($d); } ), ); $sql_details = new mysqli($dbhost, $dbuser, $dbpswd, $dbn); require( 'ssp.class.php' ); echo json_encode( SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns ) ); ?>

Answers

  • spartan2276spartan2276 Posts: 5Questions: 2Answers: 0

    I just realized that I need to bring data from 2 tables. So the bigger question is how to bring back such a query using the PHP script. The table variable only wants one table at a time.

    $table = 'estimates'

  • spartan2276spartan2276 Posts: 5Questions: 2Answers: 0

    This is my JSON Response which this damn thing is complaining about. I don't see anything wrong with it.

    {"draw":0,"recordsTotal":2,"recordsFiltered":2,"data":[{"estimateid":"1","customerid":"1","compid":"1","reference_num":"r-10001","estimate_date":"2017-07-12","expiry_date":"2017-07-19","hours":"3","sales_person":"Joe Smith","project_name":"Linux Configuration"},{"estimateid":"2","customerid":"2","compid":"1","reference_num":"a-10002","estimate_date":"2017-06-05","expiry_date":"2017-06-10","hours":"8","sales_person":"Joe smith","project_name":"Windows Server Configuration"}]}

  • kthorngrenkthorngren Posts: 20,468Questions: 26Answers: 4,804

    These are the objects in your JSON:

    {
            "estimateid": "1",
            "customerid": "1",
            "compid": "1",
            "reference_num": "r-10001",
            "estimate_date": "2017-07-12",
            "expiry_date": "2017-07-19",
            "hours": "3",
            "sales_person": "Joe Smith",
            "project_name": "Linux Configuration"
        }
    

    In your first post you define these columns:

    "columns": [
    { "data": "first_name" },
    { "data": "last_name" },
    { "data": "reference_num" },
    { "data": "estimate_date" },
    { "data": "expiry_date" },
    { "data": "hours" }
    ]
    

    Your JSON doesn't have first_name nor last_name. Not sure what error you are getting now but this will cause you problems.

    Kevin

  • spartan2276spartan2276 Posts: 5Questions: 2Answers: 0

    @kevin I fixed the JSon issue, I actually now get the datatables display but no data comes back. The PHP is also correct. Is like the browser does not see it. I'm on the latest version of version of FF and Chrome.

This discussion has been closed.