How to page in php

0

Good, I have a list of a database with the controller view model and I do not know how to paginate it

view_listUsers

<div id="cuadro">
    <div id="titulo">
        <h1>Registros</h1>
    </div>
</div>
<table>
    <thead>
        <tr class="centro">
            <td>Usuario</td>
            <td>Contraseña</td>
            <td>Nombre</td>
            <td>1º Apellido</td>
            <td>2º Apellido</td>
            <td>DNI</td>
            <td>Dirección</td>
            <td>Población</td>
            <td>Código Postal</td>
            <td>Provincia</td>
            <td>Teléfono</td>
            <td>Email</td>
            <td>Web</td>
            <td>Blog</td>
            <td>Twitter</td>
            <td>Fecha de registro</td>            
        </tr>
    <tbody>
        <?php foreach ($usuarios as $user)
        {

        ?>
        <tr>
        <td><?php echo $user['usuario'];?></td>    
        <td><?php echo $user['contraseña'];?></td>   
        <td><?php echo $user['nombre'];?></td> 
        <td><?php echo $user['apellido1'];?></td> 
        <td><?php echo $user['apellido2'];?></td> 
        <td><?php echo $user['dni'];?></td> 
        <td><?php echo $user['direccion'];?></td>
        <td><?php echo $user['poblacion'];?></td>
        <td><?php echo $user['CPostal'];?></td>
        <td><?php echo $user['provincia'];?></td>
        <td><?php echo $user['telefono'];?></td>
        <td><?php echo $user['email'];?></td>
        <td><?php echo $user['web'];?></td>
        <td><?php echo $user['blog'];?></td>
        <td><?php echo $user['twitter'];?></td>
        <td><?php echo $user['fecha'];?></td> 
        </tr>
        <?php }?>
    </tbody>
</table>

ctrl_listUsers

   include_once (MODEL_PATH.'model_usuario.php');

    $usuarios = listaUsuarios();
    include (VIEW_PATH.'view_listarUsuarios.php');

model_user

   include_once (MODEL_PATH.'claseBD.php');

function insertaUsuario($valores){
    try{
        $sentencia = 'INSERT INTO usuarios (usuario, contraseña, nombre, apellido1, apellido2, dni,direccion, '
                . 'poblacion, CPostal, provincia,telefono, email, web, blog, twitter)'
                . ' VALUES ("'.$valores['usuario'].'","'.$valores['pass'].'","'.$valores['nombre'].'","'.$valores['apellido1'].'","'.$valores['apellido2'].'",'
                . '"'.$valores['dni'].'","'.$valores['direccion'].'","'.$valores['poblacion'].'","'.$valores['CPostal'].'","'.$valores['provincia'].'","'.$valores['telefono'].'",'
                . '"'.$valores['email'].'","'.$valores['web'].'","'.$valores['blog'].'","'.$valores['twitter'].'","'.$valores['tipo'].'")';
        $Db=db::getInstance();
        $Db->Ejecutar($sentencia);    

    } catch (PDOexception $e){
        die($e->getMessage());
    }
}
function listaUsuarios(){
    try{
        $sentencia='SELECT * FROM usuarios ORDER BY fecha DESC';
        $Db=db::getInstance();
        $listado=$Db->Ejecutar($sentencia);
        return $listado;
    } catch (PDOexception $e){
        die($e->getMessage());
    }
}
function borraUsuario($usuario){
    try{
        $sentencia = 'DELETE  FROM usuarios WHERE usuario =".$usuario."';
        $Db=db::getInstance();
        $listado=$Db->Ejecutar($sentencia);
    } catch (PDOexception $e) {
        die($e->getMessage());
    }
}
    
asked by Alberto Tello 12.12.2017 в 17:02
source

1 answer

0

To use dataTable you must add the files jquery-1.12.4.min.js also jquery.dataTables.min.js and jquery.dataTables.min .css and the references.

Then you add the script , it can be like this:

<script>
$(document).ready(function(){
    $('#myTable').DataTable({  
                         
    "language": {
    "lengthMenu": "Mostrar _MENU_ registros por pagina",
      //"info": "Mostrando pagina _PAGE_ de _PAGES_ / Mostrados: _START_ de _END_ ",
    "sInfo": "Mostrando: _START_ de _END_ - Total registros: _TOTAL_ ",
    "infoEmpty": "No hay registros disponibles",
    "infoFiltered": "(filtrada de _MAX_ registros)",
    "loadingRecords": "Cargando...",
    "processing": "Procesando...",
    "search": "Buscar:",
    "zeroRecords": "No se encontraron registros coincidentes",
    "paginate": {
    "next": "Siguiente",
    "previous": "Anterior"
  },
}
//linea 137 camniar el valor de inicio del select jquery.dataTables.min.js
});
});
</script>

Where #myTable is the name of the id in your table and you add the classes to it to work correctly. For example when creating the table it would be something like this:

 <table id="myTable" class="compact display nowrap cell-border dataTable table table-striped table-hover table-condensed">
 ....
 ....
 ....
 </table>

And at the end you will have a table with search field, pagination and a select with the option of the records you want to display per page. I hope I could help what you need.

    
answered by 12.12.2017 / 18:11
source