How can I put paginator to this table in PHP? [closed]

0
    <div id="page-wrapper">
<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">REGISTRO DE  <?php echo $title ?>S</h1>
        <section>
            <button class="btn btn-success" id="add">AGREGAR <?php echo $title ?></button>

              <div class="table-responsive">
            <table class="table table-bordered table-hover">
                <thead>
                    <th class="col-sm-1">ID</th>
                    <th class="col-sm-1">RAZON SOCIAL</th>
                    <th class="col-sm-1">RUC</th>
                    <th class="col-sm-1">FECHA INSCRIPCION</th>
                    <th class="col-sm-1">COMENTARIOS</th>


                    <th class="col-sm-2" colspan="2"><center>ACCIONES</center></th>
                </thead>
                <tbody>
                    <?php 

                    if (mysqli_num_rows($result)>0)
                    {
                        while ($fila = $result->fetch_array()) 
                        {
                    ?>
                    <input type="hidden" id="v0<?php echo $fila['0'] ?>" value="<?php echo $fila['razonsocial'] ?>">
                    <input type="hidden" id="v1<?php echo $fila['0'] ?>" value="<?php echo $fila['ruc'] ?>">
                    <input type="hidden" id="v2<?php echo $fila['0'] ?>" value="<?php echo $fila['fechainscripcion'] ?>">
                    <input type="hidden" id="v3<?php echo $fila['0'] ?>" value="<?php echo $fila['comentarios'] ?>">

                    <tr>
                        <td class="info"><?php echo $fila['0']?></td>
                        <td class="success"><?php echo $fila['razonsocial']?></td>
                        <td class="success"><?php echo $fila['ruc']?></td>
                        <td class="success"><?php echo $fila['fechainscripcion']?></td>
                        <td class="success"><?php echo $fila['comentarios']?></td>
                        <td>
                            <input type="button" class="btn btn-primary" id="regUpdate" data-target="#modalUpdate" data-toggle="modal" value="Editar" onclick="regUpdate(<?php echo $fila['0'] ?>)">
                        </td>
                        <td>
                            <input type="button" onclick="regDelete(<?php echo $fila['0'] ?>)" class="btn btn-danger" value="Borrar">
                        </td>                       
                    </tr>                                    
                    <?php

                        } 
                    } 
                    else 
                    {
                    ?>
                    <td colspan = "8"><h4>VAYA! No se encontraron registros</h4></td>
                    <?php
                    } 
                    ?>

                </tbody>
            </table>
            </script>
            </div>
        </section>
    </div>
    
asked by Alexis 03.01.2019 в 19:52
source

1 answer

0

I thought some time ago this small and simple page system is a matter that you use and modify your needs

Attention: it does not have an anti-bug system, attack among others.

<?php 
/* PAGINACION SIMPLE PHP */
//conexion a DB
$conexion = mysqli_connect('localhost', 'usuario', 'password', 'base_datos');
if(!$conexion){
    echo 'Error al conectar';
    die();
}

//setear caracteres
mysqli_set_charset($conexion, 'utf8');

//cantidad por pagina
$porpagina = 6;

//máximo de botones
$botones = 6;

//indice paginación
if(!isset($_GET['pagina'])) $indice = 0 ; else $indice = ($_GET['pagina']-1) * $porpagina;

//total de posts
$totalpost = $conexion->query("SELECT * FROM mi_tabla");

//obtener el numero de paginas
$paginas = $totalpost->num_rows / $porpagina;

//consulta a tabla
$consulta = $conexion->query("SELECT * FROM mi_tabla ORDER BY id DESC LIMIT $indice, $porpagina");


?>
<div class="paginacion">

<!-- listado posts -->
<h1>Listado de posts</h1>
<ul>

<?php if( $consulta->num_rows > 0 ){
while( $res = $consulta->fetch_array() ){ ?>
        <!-- $res['id'] y $res['titulo'] son columnas de la tabla mi_tabla -->
        <li>#<?php echo $res['id'] ?>: <?php echo $res['titulo'] ?></li>
    <?php 
} }?>
</ul>

<!-- botones -->
<?php if( $consulta->num_rows > 0 ){

//botón principal
$inicioboton = 1;
if( isset($_GET['pagina']) ){
    $inicioboton = $_GET['pagina']-1;
    if( $_GET['pagina']==1 ){
        $inicioboton = $_GET['pagina'];
    }
}

//calcular cantidad botones mediante el botón de inicio
$botones = $botones + $inicioboton;

//calcular el fin de los botones
if( ($botones) > $paginas ){
    $botones = $paginas + 1;
} 

?>

<!-- Retroceder 1 pagina -->
<?php if( isset($_GET['pagina']) && $_GET['pagina'] > 1 ) {?>
<span><a href="?pagina=<?php echo $_GET['pagina'] - 1; ?>">&laquo;</a></span>
<?php } ?>

<?php
//bucle para mostrar botones
for ( $i= $inicioboton; $i < $botones; $i++ ) { ?>
    <span><a href="?pagina=<?php echo $i; ?>"><?php echo $i; ?></a></span>
<?php } } ?>

<!-- Avanzar 1 pagina -->
<?php if( !isset($_GET['pagina']) || $_GET['pagina'] < $paginas ) {?>
<span><a href="?pagina=<?php if( isset($_GET['pagina']) ) echo $_GET['pagina'] + 1; else echo 2; ?>">&raquo;</a></span>
<?php } ?>

</div>
    
answered by 03.01.2019 / 20:45
source