<?php
# navegacao.php
require_once("connection.php"); // $connect
$table="contactos";
$sql="SELECT id FROM $table";
$queryTotal=mysqli_query($connect, $sql);
$tr=mysqli_num_rows($queryTotal); // total de Registos
if(!isset($_GET['limite'])){
$limite=2;
} else {
$limite=$_GET['limite'];
}
// porque ainda não usei a paginação
if(!isset($_GET['mais']) || !isset($_GET['menos']) || !isset($_GET['end'])){
$inicio=0;
}
// porque já estou a usar a paginação
if(isset($_GET['mais'])){
$inicio=$_GET['inicio']+$limite;
}
if(isset($_GET['menos'])){
$inicio=$_GET['inicio']-$limite;
}
if(isset($_GET['end'])){
$inicio=$tr-1; // sai um par de orelhas de burro!!!
}
$ini=$inicio+1;
$sql="SELECT id, contacto FROM $table LIMIT $inicio, $limite";
$query=mysqli_query($connect, $sql);
$fetch=mysqli_fetch_assoc($query);
?>
<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<title>Navegação</title>
</head>
<body>
<table border="1" cellpadding="5px">
<tr>
<th>ID</th>
<th>Contacto</th>
</tr>
<?php do {?>
<tr>
<td><?php echo $fetch['id'];?></td>
<td><?php echo $fetch['contacto'];?></td>
</tr>
<?php } while($fetch=mysqli_fetch_assoc($query)); ?>
</table>
<section>
<a <?php if($ini>1){?>
href="?&limite=<?php echo $limite;?>" <?php }?>>Home</a>
<a <?php if($ini>1){?>
href="?menos&inicio=<?php echo $inicio;?>&limite=<?php echo $limite;?>"
<?php }?>>Previous</a>
<a <?php if($ini<=$tr-$limite){?>
href="?mais&inicio=<?php echo $inicio;?>&limite=<?php echo $limite;?>"
<?php }?>>Next</a>
<a <?php if($ini<=$tr-$limite){?>
href="?end&limite=<?php echo $limite;?>" <?php }?>>End</a>
</section>
<section><p><?php echo "Registo $ini de $tr";?></p></section>
<form id="alterarLimite">
<select name="limite" onchange="document.getElementById('alterarLimite').submit();">
<option value="<?php echo $limite;?>"><?php echo $limite;?></option>
<?php if($limite!=2){?>
<option value="2">2</option>
<?php }?>
<?php if($limite!=3){?>
<option value="3">3</option>
<?php }?>
</select>
</form>
</body>
</html>
Sem comentários:
Enviar um comentário