Skip to content

Instantly share code, notes, and snippets.

@evilnapsis
Created December 23, 2016 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evilnapsis/9e7af2c49fd1150241b0d46bd9bb86ea to your computer and use it in GitHub Desktop.
Save evilnapsis/9e7af2c49fd1150241b0d46bd9bb86ea to your computer and use it in GitHub Desktop.
Ejemplo de tabla para paginacion en PHP
<?php
include "conexion.php";
$limitpage = 10;
$page = 1;
if(isset($_GET["page"]) && $_GET["page"]!=""){ $page=$_GET["page"]; }
$startpage = 0;
$endpage = $limitpage;
if($page>1){ $startpage=($page-1)*$limitpage;$endpage=($page)*$limitpage; }
//echo $startpage;
$user_id=null;
$sql0= "select count(*) as c from person";
$query0 = $con->query($sql0);
$count = $query0->fetch_array();
$npages = $count["c"]/$limitpage;
$sql1= "select * from person limit $startpage,$limitpage";
$query = $con->query($sql1);
?>
<?php if($query->num_rows>0):?>
<h4>Pagina <?php echo $page; ?> de <?php echo ceil($npages); ?></h4>
<table class="table table-bordered table-hover table-condensed">
<thead>
<th>Id</th>
<th>Nombre</th>
<th>Apellido</th>
<th>Email</th>
<th>Direccion</th>
<th>Telefono</th>
<th></th>
</thead>
<?php while ($r=$query->fetch_array()):?>
<tr>
<td><?php echo $r["id"]; ?></td>
<td><?php echo $r["name"]; ?></td>
<td><?php echo $r["lastname"]; ?></td>
<td><?php echo $r["email"]; ?></td>
<td><?php echo $r["address"]; ?></td>
<td><?php echo $r["phone"]; ?></td>
<td style="width:150px;">
<a href="./editar.php?id=<?php echo $r["id"];?>" class="btn btn-sm btn-warning">Editar</a>
<a href="#" id="del-<?php echo $r["id"];?>" class="btn btn-sm btn-danger">Eliminar</a>
<script>
$("#del-"+<?php echo $r["id"];?>).click(function(e){
e.preventDefault();
p = confirm("Estas seguro?");
if(p){
window.location="./php/eliminar.php?id="+<?php echo $r["id"];?>;
}
});
</script>
</td>
</tr>
<?php endwhile;?>
</table>
<?php if($count["c"]>$limitpage):?>
<ul class="pagination">
<?php if($page>1):?>
<li>
<a href="<?php if($page>2):?>./index.php?page=<?php echo $page-1; ?><?php else:?> ./index.php <?php endif; ?>" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li><a href="index.php">1</a></li>
<?php endif; ?>
<?php
for($i=2;$i<$npages+1;$i+=1):?>
<li><a href="index.php?page=<?php echo $i;?>"><?php echo $i; ?></a></li>
<?php endfor; ?>
<?php if($page<$npages):?>
<li>
<a href="index.php?page=<?php echo $page+1;?>" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
<?php endif; ?>
</ul>
<?php endif; ?>
<?php else:?>
<p class="alert alert-warning">No hay resultados</p>
<?php endif;?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment