Lately I’ve been trying to make a good pagination program (a VERY basic one). This it’s the code:
<?php
/* Pagination program */
#Getting the var value form the URL
$page = $_GET['page'];
#If the user enters to pag.php its going to be "redirected" to pag.php?
page=1
if(!$page) {
$page = 1;
} else {
$offset = ($page -1)*itemsPerPage;
}
#Connecting to MySQL
$conexion = mysqli_connect("localhost","root",$password) or die(mysqli_error());
#Selecting DB
$db = mysqli_select_db($conexion,"users") or die(mysqli_error());
#Initial query.
$query = mysqli_query($conexion,"select nick from users order by nick") or die(mysqli_error());
#Basic Vars
$total = mysqli_num_rows($query); //Total itmes.
$itemsPerPage = 5; //Itmes per page I wish to show.
$numPages = ceil($total/$itemsPerPage); // Total pages to paginate.
/*$offset =($page - 1) * itemsPerPage; //These says to MySQL where to start looking (LIMIT sql sentence)*/
#Second query.
$query2 = mysqli_query($conexion,"select nick from usuarios order by nick limit $offset, $itemsPerPage") or die(mysqli_error());
#Showing data
while ($row = mysqli_fetch_array($query2)) {
extract($row);
echo "$nick<br>";
}
#Making LINKS |Page 1|Page 2|Page 3|Page 4|...etc
for ($i=1;$i<=$numPages;$i++) {
echo "|";
if($i == $page) {
echo "<b>Page $i </b>";
}
else {
echo "<a href='pag.php?page=$i'>Page $i </a>";
}
}
?>
Well, that’s all. It stars working perfectly, it shows me 5 elements from de DB, the buttons |Page 1|Page 2 |Page 3| also shows, the problem comes when clicking to other link, the 5 new elements that supose to appear from the DB doesn’t appear… instead of that the older ones still there. The URL shows the var (pag.php?page=2, pag.php?page=3…etc) but its seems that the mysqli_query doesnt care.
Any help it will be very helpful.