pagination

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
etsted
Forum Newbie
Posts: 15
Joined: Thu Feb 06, 2014 12:33 pm

pagination

Post by etsted »

does anyone have a good pagination video i can watch, i try to find one but no one of them works on my browser, it keeps saying: This webpage has a redirect loop.

eksampel:

include "connect.php";

$per_page = 5;
$pages_query = mysql_query("SELECT COUNT(id) FROM bruker") or die(mysql_error());
$pages = ceil(mysql_result($pages_query, 0) / $per_page);

if(!isset($_GET['pages'])) {
header("location: pagination.php?page=1");
} else {
$page = $_GET['page'];
}

$start = (($page - 1) * $per_page);
$bruker = mysql_query("SELECT * FROM bruker LIMIT $start, $per_page");
while($row = mysql_fetch_assoc($bruker)) {
$bruker = $row['bruker'];

echo $bruker . "<br />";
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pagination

Post by Celauran »

etsted wrote:does anyone have a good pagination video i can watch, i try to find one but no one of them works on my browser, it keeps saying: This webpage has a redirect loop.

Code: Select all

if(!isset($_GET['pages'])) {
    header("location: pagination.php?page=1");
There's your loop. You're looking for 'pages' and setting 'page'.

Rather than redirecting, why don't you just set $page to 1 if it isn't set?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: pagination

Post by requinix »

Keep in mind that a header() will not stop your script. It will keep running. So with what you have now, if the page isn't set then it'll add that redirect header yeah, but it'll then continue on to try to execute the SELECT query and all that. But with $page not set, $start = (null - 1) * 5 = -5 and you end up with an invalid

Code: Select all

SELECT * FROM bruker LIMIT -5, 5
And +1 to what Celauran said: don't bother with a redirect and just assume the default $page = 1. And while you're in there you should validate the number - don't want people putting ?page=-1 or ?page=notanumber to screw up your code.
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: pagination

Post by pbs »

Post Reply