limit show datatable ajax

  • Creatore Discussione Creatore Discussione #Fede5
  • Data di inizio Data di inizio

#Fede5

Nuovo Utente
23 Ott 2019
11
0
1
Ciao ragazzi, ho un problema con la visualizzazione del numero di righe della tabella. La tabella viene visualizzata sempre tutta anche se chiedo di mostrarla a 10 righe per volta. Vi allego il mio codice, magari qualcuno riesce ad aiutarmi...grazie

HTML:
<!DOCTYPE html>
<html>
 <head>
  <title>Progetto</title>
 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script> 
  <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

 </style>
 </head>
 <body>

<table id="user_data" class="table table-bordered table-striped">
     <thead>
      <tr>
    
       <th width="35%">Cognome</th>
       <th width="35%">Nome</th>
       <th width="10%">Conferma</th>   
       <th width="10%">Adulti</th>     
       <th width="10%">Bambini</th>
       <th width="10%">Modifica</th>
       <th width="10%">Cancella</th>
      </tr>
     </thead>
    </table>
  
  
 </body>
</html>


<script type="text/javascript" language="javascript" >
$(document).ready(function(){       
 
 
 var dataTable = $('#user_data').DataTable({
  "processing":true,
  "serverSide":true, 
  "order":[],
  "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "Tutti"]],     
  "ajax":{
   url:"fetch.php",
   type:"POST"
  },
  "columnDefs":[
   {
    "targets":[0,1,2, 3, 4, 5, 6],
    "orderable":false,
   },
  ],
        
 });


e fetch.php

PHP:
<?php
include('db.php');
include('function.php');
$query = '';
$output = array();
$query .= "SELECT * FROM prenotazione ";
if(isset($_POST["search"]["value"]))
{
 $query .= 'WHERE Cognome LIKE "%'.$_POST["search"]["value"].'%" ';
 $query .= 'OR Nome LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(isset($_POST["order"]))
{
 $query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].'' ;
  
}
else
{
 $query .= 'ORDER BY Cognome ASC, Nome ASC';
}


if($_POST["length"] != -1){
 
$query .= 'LIMIT' .$_POST['start'] . ',' .$_POST['length'];

}

$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
 
 $sub_array = array();
 
 $sub_array[] = $row["Cognome"];
 $sub_array[] = $row["Nome"];
 $sub_array[] = $row["Conferma"];
 $sub_array[] = $row["Adulti"];   
 $sub_array[] = $row["Bambini"];     
 $sub_array[] = '<button type="button" name="update" id="'.$row["id"].'" class="btn btn-warning btn-xs update">Modifica</button>';
 $sub_array[] = '<button type="button" name="delete" id="'.$row["id"].'" class="btn btn-danger btn-xs delete">Cancella</button>';
 $data[] = $sub_array;

}
$output = array(
 "draw"    => intval($_POST["draw"]),
 "recordsTotal"  =>  $filtered_rows,
 "recordsFiltered" => get_total_all_records(),
 "data"    => $data
);
echo json_encode($output);
?>


se dal PHP tolgo la linea di codice
PHP:
if($_POST["length"] != -1){

$query .= 'LIMIT' .$_POST['start'] . ',' .$_POST['length'];

}

visualizza la tabella completa e il tasto Show 10/25/50/Tutti non ha nessun effetto mentre se la tengo nel codice non mi viene visualizzata neanche la tabella. Suggerimenti?
 

Discussioni simili