Foros del Web » Programando para Internet » PHP »

Mostrar resultado por ID seleccionado

Estas en el tema de Mostrar resultado por ID seleccionado en el foro de PHP en Foros del Web. Hola Grupo, Espero me puedan colaborar, necesito resolver este detalle, estoy usando PHP "DataTable" y Bootstrap, donde me muestra todo los comentarios en general de ...
  #1 (permalink)  
Antiguo 21/09/2022, 18:05
 
Fecha de Ingreso: noviembre-2004
Ubicación: NULL
Mensajes: 652
Antigüedad: 19 años, 4 meses
Puntos: 6
Sonrisa Mostrar resultado por ID seleccionado

Hola Grupo,

Espero me puedan colaborar, necesito resolver este detalle,
estoy usando PHP "DataTable" y Bootstrap, donde me muestra todo los comentarios en general de la base de datos.
la idea es que al seleccionar un post.. me muestre solo los comentarios de ese dicho post,
la tabla "feedbacks" tiene el ID de la tabla "post" post_id

Se, me ocurre algo así, pero tampoco me resulto "myfeedback.php".

$query .= 'WHERE feedbacks_id = '".$_POST["post_id"]."' ';


Código Javascript:
Ver original
  1. Esto carga la lista de comentarios
  2. <script>
  3.   $(document).ready(function () {
  4.     //........
  5.     var dataTable = $("#MyTablaFeedback").DataTable({
  6.       paging: true,
  7.       processing: true,
  8.       serverSide: true,
  9.       order: [],
  10.       info: false,
  11.       ajax: {
  12.         url: "myfeedback.php",
  13.         type: "POST",
  14.       },
  15.       columnDefs: [
  16.         {
  17.           targets: [0, 1, 2],
  18.           orderable: false,
  19.         },
  20.       ],
  21.     });
  22.   }  
  23. </script>


MUESTRA TODO LOS COMENTARIOS "myfeedback.php"
Código PHP:
Ver original
  1. <?php
  2. include_once 'conn.php';
  3.   function get_total_all_records()  {
  4.     include('conn.php');
  5.       $statement = $conexion->prepare("SELECT * FROM feedbacks");
  6.       $statement->execute();
  7.       $result = $statement->fetchAll();
  8.     return $statement->rowCount();
  9.   }
  10. $query = '';
  11. $output = array();
  12. $query .= "SELECT * FROM feedbacks ";
  13. if(isset($_POST["search"]["value"]))
  14. {
  15.     $query .= 'WHERE msg_feedbacks LIKE "%'.$_POST["search"]["value"].'%" '; // AQUI TRAER EL ID DEL COMENTARIO AL SELECCIONAR UN POST
  16. }
  17.  
  18. if(isset($_POST["order"]))
  19. {
  20.     $query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
  21. }
  22. else
  23. {
  24.     $query .= 'ORDER BY feedbacks_id DESC ';
  25. }
  26. if($_POST["length"] != -1)
  27. {
  28.     $query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
  29. }
  30. $statement = $conexion->prepare($query);
  31. $statement->execute();
  32. $result = $statement->fetchAll();
  33. $data = array();
  34. $filtered_rows = $statement->rowCount();
  35. foreach($result as $row)
  36. {
  37.     $sub_array = array();  
  38.     $sub_array[] = $row["feedbacks_id"];
  39.     $sub_array[] = $row["msg_feedbacks"];
  40.   $sub_array[] = '<button type="button" name="viewFeedback" id="'.$row["feedbacks_id"].'" data-bs-dismiss="modal">VER COMENTARIO</button>';
  41.     $data[] = $sub_array;
  42. }
  43. $output = array(
  44.     "draw"              =>  intval($_POST["draw"]),
  45.     "recordsTotal"      =>  $filtered_rows,
  46.     "recordsFiltered"   =>  get_total_all_records(),
  47.     "data"              =>  $data
  48. );
  49. echo json_encode($output);
  50. ?>

MI LISTA DE PUBLICACIONES INDEX "fetch.php"
Código HTML:
Ver original
  1. <div class="table-responsive-md">          
  2.   <table class="table table-sm" id="MyTablaPOST">
  3.     <thead>
  4.       <tr>
  5.         <th scope="col">ID</th>
  6.         <th scope="col">Publicaciones</th>         
  7.         <th scope="col">Accion (Ver) </th>  <!-- FETCH - MODAL "Bootstrap"-->
  8.       </tr>
  9.     </thead>
  10.   </table>
  11. </div>
  12. <!-- My Modal de Feedback -->
  13. <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  14.   <div class="modal-dialog">
  15.     <div class="modal-content">
  16.       <div class="modal-header">
  17.         <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
  18.         <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  19.       </div>
  20.       <div class="modal-body">
  21.       <!-- MI LISTA DE COMANTARIOS -->
  22.       <div class="table-responsive-md">          
  23.         <table class="table table-sm" id="MyTablaFeedback">
  24.           <thead>
  25.           <tr>
  26.             <th scope="col">#</th>
  27.             <th scope="col">feedbacks</th>             
  28.           </tr>
  29.           </thead>
  30.         </table>
  31.       </div>
  32.       </div>
  33.       <div class="modal-footer">
  34.         <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
  35.       </div>
  36.     </div>
  37.   </div>
  38. </div>

fetch.php
Código PHP:
Ver original
  1. <?php
  2. include_once 'conn.php';
  3.  
  4. if(isset($_POST["myid"]))
  5. {  
  6.     $output = array();
  7.     $statement = $conexion->prepare(
  8.       "SELECT * FROM post WHERE post_id = '".$_POST["myid"]."' LIMIT 1"
  9.     );
  10.     $statement->execute();
  11.     $result = $statement->fetchAll();
  12.     foreach($result as $row)
  13.     {  
  14.         $output["post_id"] = $row["post_id"];
  15.         $output["title_mypost"] = $row["title_mypost"];
  16.     }
  17.     echo json_encode($output);  
  18. }
  19. ?>

Código Javascript:
Ver original
  1. Esto carga la lista de POST
  2. <script>
  3. //....
  4. $(document).on("click", ".viewFeedback", function () {
  5.     var myid = $(this).attr("id");
  6.     $.ajax({
  7.       url: "fetch.php",
  8.       method: "POST",
  9.       data: {myid: myid},
  10.       dataType: "json",
  11.       success: function (data) {
  12.         $("#post_id").val(data.post_id);
  13.         $(".title_mypost").html("<span> " + data.title_mypost + " </span>");
  14.         $(".modal-title").text("Info...");
  15.         $("#myid").val(myid);
  16.         $("#exampleModal").html(data);
  17.         $("#exampleModalLabel").modal("show");
  18.       },
  19.     });
  20.   });
  21. </script>

Etiquetas: datatable, datatables
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:49.