it does not order me correctly server side

it does not order me correctly server side

cris19ncris19n Posts: 55Questions: 18Answers: 0

As you can see, I am sorting by data 3 from highest to lowest and data 5 from lowest to highest.

but for some reason data 3 keeps starting from lowest to highest.

I do not know why it does not work

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    With server side processing enabled the sorting, searching and paging functions are the responsibility of your server script. Are you using a Datatables provided script? The server script will need to be debugged to find the problem.

    Kevin

  • cris19ncris19n Posts: 55Questions: 18Answers: 0
    edited October 2020

    this is my php code

    <?php
    
    require_once 'conect.php';
    
    ## Leer valor
    $draw = $_POST['draw'];
    
    $row = $_POST['start'];
    
    $rowperpage = $_POST['length']; // Visualización de filas por página
    
    $columnIndex = $_POST['order'][0]['column']; // Índice de columna
    
    $columnName = $_POST['columns'][$columnIndex]['data']; //Nombre de la columna
    
    $columnSortOrder = $_POST['order'][0]['dir']; // asc o desc
    
        $searchValue = $_POST['search']['value']; // Valor de búsqueda
    
    $searchArray = array();
    
    ## Buscar
    $searchQuery = " ";
    if($searchValue != ''){
        $searchQuery = " AND (lote LIKE :lote) ";
        $searchArray = array(
            'lote'=>"%$searchValue%"
        );
    }
    
    ## Número total de registros sin filtrar
    $stmt = $conn->prepare("SELECT COUNT(*) AS allcount FROM tabledts ");
    $stmt->execute();
    $records = $stmt->fetch();
    $totalRecords = $records['allcount'];
    
    ## Número total de registros con filtrado
    $stmt = $conn->prepare("SELECT COUNT(*) AS allcount FROM tabledts WHERE 1 ".$searchQuery);
    $stmt->execute($searchArray);
    $records = $stmt->fetch();
    $totalRecordwithFilter = $records['allcount'];
    
    ## Obtener registros
    $stmt = $conn->prepare("SELECT * FROM tabledts WHERE 1 ".$searchQuery." ORDER BY ".$columnName." ".$columnSortOrder." LIMIT :limit,:offset");
    
    // Vincular valores
    foreach($searchArray as $key=>$search){
        $stmt->bindValue(':'.$key, $search,PDO::PARAM_STR);
    }
    
    $stmt->bindValue(':limit', (int)$row, PDO::PARAM_INT);
    $stmt->bindValue(':offset', (int)$rowperpage, PDO::PARAM_INT);
    $stmt->execute();
    $empRecords = $stmt->fetchAll();
    
    $code=null;
    
    $data = array();
    
    foreach($empRecords as $key=> $row){
    
        if($usuarios2["tipo_usuario_pr"]=="admin"){
            $data[]= array(
                "cod_table"=>$row["cod_table"],
                1=>$row["fecha_reg"],
                2=>$row["persona_resp"],
                3=>$row["lote"],
                4=>$row["lote_mp"],
                5=>$row["viaje"],
                6=>$row["fecha_ing"],
                9=>$row["sexo"],
                10=>$row["linea"],
                11=>'
                <div class="btn-group" role="group" aria-label="Third group">
                    <button class="btn btn-warning btnEdittabledts  manito-clic" edit="'.$row["cod_table"].'" idUsuario="'.$usuarios2["id_pers_resp"].'" User="'.$usuarios2["user"].'" data-toggle="modal" data-target="#modalEditartable"><i class="fa fa-pencil"></i></button>
                </div>
                <div class="btn-group" role="group" aria-label="Third group">
                    <button class="btn btn-danger btnEliminartabledts manito-clic" delete="'.$row["cod_table"].'" lotep="'.$row["lote"].'" viajep="'.$row["viaje"].'"><i class="fa fa-times"></i></button>
                </div>'
    
            );
        }
    }
    
    if ($data==null) {
        $data = [];
    }
    
    ## Response
    $response = array(
        "draw" => intval($draw),
        "iTotalRecords" => $totalRecords,
        "iTotalDisplayRecords" => $totalRecordwithFilter,
        "aaData" => $data
    );
    
    echo json_encode($response);
    
  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin
    Answer ✓

    Just to confirm my understanding of the issue - the problem is that it is not doing multi-column sorting as you expect?

    The reason for that is that your SQL query is only doing a single column sort, and as Kevin says, when server-side processing, it is the responsibility of the server script to do the sort:

    ORDER BY ".$columnName." ".$columnSortOrder."
    

    So to do multi-column sorting, you would need to handle the array of sorting data that DataTables submits.

    This is the method in our demo SSP script to handle that, which might be of some use to you.

    Allan

This discussion has been closed.