Page 1 of 1

pagination

Posted: Thu Feb 06, 2014 1:06 pm
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 />";
}

Re: pagination

Posted: Thu Feb 06, 2014 1:10 pm
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?

Re: pagination

Posted: Thu Feb 06, 2014 2:11 pm
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.

Re: pagination

Posted: Thu Feb 06, 2014 11:23 pm
by pbs