I have a system that allows you to upload documents and for each document write your comments, this part already works but I need to show the comments that were written for that document using Datatable This is my code:
Table.php
This is the table with id="comment_table" and shows the data of my datatable
<table id="tabla_comentarios" class="table display responsive no-wrap" width="100%">
<thead>
<tr>
<th>#</th>
<th>Titulo</th>
<th>Comentarios</th>
<th>Autor</th>
<th>Fecha</th>
</tr>
</thead>
</table>
Through this variable I know what document is $ idDocument = $ _ GET ['id'];
Datatable.js
In another file, I have the definition of my datatable, in the ajax part I get all the comments, I think the solution would be to send a variable with the id of the document.
var tabla_comentarios;
$(document).ready(function() {
tabla_comentarios = $("#tabla_comentarios").DataTable({
"responsive": true,
"ajax": "../../phpcrud/select_comentarios.php",
"order": [],
"scrollX": true,
dom: 'Bfrtip',
buttons:
[
{ extend: 'copy', text: 'Copiar'},
{ extend: 'excel', text: 'Excel' },
{ extend: 'pdf', text: 'Pdf'},
{ extend: 'print', text: 'Imprimir' },
{ extend: 'csv', text: 'Csv'},
{ extend: 'colvis', text: 'Eliminar Columnas' }
]
});
});
Select_comentarios.php
<?php
require_once '../php/db_connect.php';
$output = array('data' => array());
$idDocument=$_POST['idDocument'];
$sql = "SELECT * FROM comentarios";
$query = $connect->query($sql);
$x = 1;
while ($row = $query->fetch_assoc()) {
$output['data'][] = array(
$x,
$row['titulo'],
$row['comments'],
$row['idusuario'],
$row['fecha'],
);
$x++;
}
$connect->close();
echo json_encode($output);