PDA

View Full Version : Pagination Problem



The Stranger
March 9th, 2006, 01:13 PM
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.

Voccart
March 9th, 2006, 06:29 PM
you got this on top of you php file...so the file is expecting $itemsPerPage value....but in your case its recieving nothing.


#If the user enters to pag.php its going to be "redirected" to pag.php?
// page=1 = not needed..
if(!$page) {
$page = 1;
} else {
$offset = ($page -1)*$itemsPerPage;
// you forgot a $ in itemsPerPage
}




so put this line on top of you php file :

$itemsPerPage = 5; //Itmes per page I wish to show.
and delete the same line on the bottom of your script
greets

DHDesign
March 9th, 2006, 06:29 PM
well first of all, you have:



#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;
}


you say at the start, page = 1....invalid statement....$page=1 is what you want with a ; at the end too.

second, i dont get why u are setting page to 1, then if(!$page) again, set it to one..why set it twice?

last, in the script above, u are actually going back to page one all the time because no matter what, $page is always set to 1. so $offset = (1-1)*itemsPerPage;

i can have a pagination script that works real well for me....if u want, i can post it for u.

DHDesign
March 9th, 2006, 06:37 PM
voccart is right too...it cant find a value for the itemsPerPage variable...i didnt even see that.

The Stranger
March 10th, 2006, 11:34 AM
The code I just send to you was all mess... The final problem was that I forgot the "$" in the itemsPerPage...hehe, what a typesetter error can make, no?... Well guys you really help me to figure it out, thank you!