Pagination Tutorial

Pagination Tutorial

am 16.10.2007 13:40:32 von Alec

Newbie question.

I have recently tried a php tutorial on pagination, but I just cannot
get the links to work. See www.freeweekends.co.uk/pagtest2.php. The
search finds over 60 results, but only the first page of ten are
displayed, with the Prev and Next links not working.

Can anyone help, or has anyone another pagination tutorial?

Thanks

Alec

$connection = @mysql_connect('localhost', '*****', '*****');
if (!$connection) {
echo '

Unable to make database connection.

';
exit();
}
if (!@mysql_select_db('uks49179')) {
exit('

Unable to locate database.

');
}

$limit = 10;
$query_count = "SELECT count(*) FROM companyid_uks49179 WHERE
town='Bury St. Edmunds' AND category='sleep' AND priority='0'";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);

if(empty($page)){
$page = 1;
}

$limitvalue = $page * $limit - ($limit);
$query = "SELECT company FROM companyid_uks49179 WHERE town='Bury
St. Edmunds' AND category='sleep' AND priority='0' LIMIT $limitvalue,
$limit";
$result = mysql_query($query) or die("Error: " . mysql_error());

if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}

$bgcolor = "#E0E0E0"; // light gray

echo("");

while($row = mysql_fetch_array($result)){

echo $row['company']."
";

}

echo("
");

if($page != 1){
$pageprev = $page--;

echo("
");
}else{
echo("PREV".$limit." ");
}

$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo(" ");
}
}

if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo(" ");
}
}

if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;

echo("NEXT".$limit." a>");
}else{
echo("NEXT".$limit);
}

mysql_free_result($result);

?>

Re: Pagination Tutorial

am 16.10.2007 14:33:09 von luiheidsgoeroe

On Tue, 16 Oct 2007 13:40:32 +0200, Alec wrote:
> I have recently tried a php tutorial on pagination, but I just cannot
> get the links to work. See www.freeweekends.co.uk/pagtest2.php. The
> search finds over 60 results, but only the first page of ten are
> displayed, with the Prev and Next links not working.
>
> Can anyone help, or has anyone another pagination tutorial?
>
> Thanks
>
> Alec
>
> > $connection =3D @mysql_connect('localhost', '*****', '*****');
> if (!$connection) {
> echo '

Unable to make database connection.

';
> exit();
> }
> if (!@mysql_select_db('uks49179')) {
> exit('

Unable to locate database.

');
> }
>
> $limit =3D 10;
> $query_count =3D "SELECT count(*) FROM companyid_uks49179 WHERE=

> town=3D'Bury St. Edmunds' AND category=3D'sleep' AND priority=3D'0'";
> $result_count =3D mysql_query($query_count);
> $totalrows =3D mysql_num_rows($result_count);

$totalrows here should become FALSE or 1 according to the code, never, =

ever, anything else.

$query_count =3D "SELECT count(*) FROM companyid_uks49179 WHERE =

town=3D'Bury St. Edmunds' AND category=3D'sleep' AND priority=3D'0'";
$result_count =3D mysql_query($query_count);
$totalrows =3D mysql_result($result_count,0,0);


> if(empty($page)){

register_globals should be disabled, and is probably the problem. Or hav=
e =

you filled $page somewhere else?

> $page =3D 1;
> }
>
> $limitvalue =3D $page * $limit - ($limit);

I certainly hope you check $page > 0?
And make sure it's an integer, floats in limit clauses can't be the goal=
:)
-- =

Rik Wasmus