Select Where datatables with php and Ajax

0

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);
    
asked by Rastalovely 29.03.2017 в 17:36
source

1 answer

1

I think what you're asking is how to send the data to $ _POST ['idDocument'] in select_comentarios.php:

'ajax': {
    'url': '../../phpcrud/select_comentarios.php',
    'type': 'POST', 
    'data': {
       idDocument: variable_con_ID_documento,
    },
}

And if you want to get it for $ _GET, then use the GET method.

But I do not understand what you want to do, if it is a page with a document and its related comments, it would be better to print everything to the first one with a single query to the BDD and then convert the normal table to Datatables, I do not see the need to a second query by AJAX, unless you have thousands of comments for each document and want to page them.

    
answered by 29.03.2017 / 20:53
source