I have a problem that when loading data from a MySQL database with PHP, it does not show the data, which I send with a JSON according to documentation of Datatables loading data with Ajax then I present my files.
Below I present my files.
HTML
<table id="table-study" class="responsive-table striped highlight">
<thead>
<tr>
<th>ID</th>
<th>Nombre del estudio</th>
</tr>
</thead>
</table>
Model where it shows the function that obtains the records of that table, where the function getRowsAjax, executes the sql and is where the json_encode () applies to pass all the records to JSON
// Obtener estudios
public function getStudy()
{
$sql = "SELECT codi_estu,nomb_estu FROM estudio ORDER BY codi_estu";
$params = array(null);
return Database::getRowsAjax($sql, $params);
}
File Database getRowsAjax
public static function getRowsAjax($query, $values)
{
self::connect();
self::$statement = self::$connection->prepare($query);
self::$statement->execute($values);
self::desconnect();
return '{"data":'.json_encode(self::$statement->fetchAll(PDO::FETCH_ASSOC)).'}';
}
If they are set there concatenate the "data", but now according to the documentation can also be done without that and would suffice with the following:
return json_encode(self::$statement->fetchAll(PDO::FETCH_ASSOC));
JavaScript
$(document).ready(function () {
dataTable();
});
function dataTable()
{
$('#table-study').DataTable({
ajax: {
url: '../dashboard/study.php',
dataSrc: 'data'
},
columns: [
{ data : 'codi_estu' },
{ data : 'nomb_estu' }
],
language: {
"sProcessing": "Procesando...",
"sLengthMenu": "Mostrar _MENU_ registros",
"sZeroRecords": "No se encontraron resultados",
"sEmptyTable": "Ningún dato disponible en esta tabla",
"sInfo": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
"sInfoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
"sInfoFiltered": "(filtrado de un total de _MAX_ registros)",
"sInfoPostFix": "",
"sSearch": "Buscar:",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "Cargando...",
"oPaginate": {
"sFirst": "Primero",
"sLast": "Último",
"sNext": "Siguiente",
"sPrevious": "Anterior"
},
"oAria": {
"sSortAscending": ": Activar para ordenar la columna de manera ascendente",
"sSortDescending": ": Activar para ordenar la columna de manera descendente"
}
},
iDisplayLength: 5
});
$('select').formSelect();
}
Now in the url in study.php the only thing I have is that I require the controller
require_once '../config/app.php';
require_once APP_PATH . '/app/Controllers/StudyController.php';
Driver
<?php
require_once APP_PATH . '/app/models/Study.php';
try {
$study = new Study;
$rows = $study->getStudy();
echo $rows;
// Requiriendo vista
require_once APP_PATH . '/views/study/index.view.php';
} catch (Exception $error) {
Page::showMessage(2, $error->getMessage(),null);
}
The echo sends the following JSON
{
"data":
[
{
"codi_estu":"1",
"nomb_estu":"VCN MINF"
},
{
"codi_estu":"2",
"nomb_estu":"EMG"
}
]
}
Which to some extent all right, there is no problem, but when I reload the page the warning: DataTables warning: table id=table-study - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
What I did was to go to the url that the alert tells me and I did what it says there, to inspect the element, go to the Network section and select XHR and then reload the page and select the Response option, where the warnings and / or errors should come out, but it does not indicate anything to me
As you can see in the attached image, the label <tbody></tbody>
And until then I have stayed in front of you many thanks for reading