[SOLVED] Loading fast

[SOLVED] Loading fast

simotuxsimotux Posts: 19Questions: 4Answers: 1
edited October 2016 in Free community support

Hello,

I started to use DataTable with 500 data and the json loading data was perfect to me, but i have more than 1000+ data to load and the drawing is slower, of course. I read about server side loading but I don't have mind how to change my code to avoid issues.

This is the fiddle: https://jsfiddle.net/m4fccz34/
This is the api_ricercautente_nl:

```
<?php
$type=getPar_empty("type",$_GET,"");
$out=array();
$stmt=null;
if ($type=="ricerca_utenti"){
$stmt=null;
$stmt=$db->prepare("select id,email,nome,cognome,lingua,unsubscribe from newsletter_utenti");
if (!$stmt) {
log_msg($db->error);
die();
}
$stmt->execute();
$row=fetchArray2($stmt);
$stmt->close();
$out['utenti'] = $row;
$db->close();
echo json_encode($out);
exit;
}
$db->close();
echo json_encode($out);

<?php > ``` ?>

Thank you!

This question has an accepted answers - jump to answer

Answers

  • simotuxsimotux Posts: 19Questions: 4Answers: 1

    EDIT: I'm following this tutorial https://coderexample.com/datatable-demo-server-side-in-phpmysql-and-ajax/ but i receive the error: "TypeError: Cannot read property 'length' of undefined".

    What's wrong? I'm so sorry but I'm a newbie.

    search_user.php

    if ($type=="search"){
        $requestData= $_REQUEST;
        $columns = array(
            0 =>'id',
            1 =>'email',
            2 =>'nome',
            3 =>'cognome',
            4 =>'lingua',
            5 =>'unsubscribe'
        );
        $sql = "SELECT id,email,nome,cognome,lingua,unsubscribe FROM newsletter_utenti";
        $query=mysqli_query($db, $sql) or die();
        $totalData = mysqli_num_rows($query);
        $totalFiltered = $totalData;
    
        $data = array();
        while( $row=mysqli_fetch_array($query) ) {
            $nestedData=array();
            $nestedData[] = $row["id"];
            $nestedData[] = $row["email"];
            $nestedData[] = $row["nome"];
            $nestedData[] = $row["cognome"];
            $nestedData[] = $row["lingua"];
            $nestedData[] = $row["unsubscribe"];
            $data[] = $nestedData;
        }
        $json_data = array(
                    "draw"            => intval( $requestData['draw'] ),
                    "recordsTotal"    => intval( $totalData ),
                    "recordsFiltered" => intval( $totalFiltered ),
                    "data"            => $data
                    );
    
        echo json_encode($json_data);
    }
    

    JS Script

    var dataTable = $('#contacts-grid').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax":{
          url : "search_user.php",
          type: "post",
          data: {type:'search'},
          error: function(){  // error handling
            alert("error");
    
          }
        }
      } );
    

    HTML

    <table class="table table-responsive filter-head" id="contacts-grid">
                      <thead>
                        <tr>
                          <th>ID</th>
                          <th>E-mail</th>
                          <th>Nome</th>
                          <th>Cognome</th>
                          <th>Lingua</th>
                          <th>Stato</th>
                        </tr>
                      </thead>
                      <tbody></tbody>
                    </table>
    

    Thank you!

  • simotuxsimotux Posts: 19Questions: 4Answers: 1
    Answer ✓

    I deleted if ($type=="search"){ and now it works! :)

This discussion has been closed.