Pagination Tutorial

This is a discussion on Pagination Tutorial within the PHP Language forums, part of the PHP Programming Forums category; Newbie question. I have recently tried a php tutorial on pagination, but I just cannot get the links to work. ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-16-2007
Alec
 
Posts: n/a
Default Pagination Tutorial

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

<?php
$connection = @mysql_connect('localhost', '*****', '*****');
if (!$connection) {
echo '<p>Unable to make database connection.</p>';
exit();
}
if (!@mysql_select_db('uks49179')) {
exit('<p>Unable to locate database.</p>');
}

$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("<table>");

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

echo $row['company']."<br />";

}

echo("</table>");

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

echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV".$limit."</a>
");
}else{
echo("PREV".$limit." ");
}

$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}

if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}

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

echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</
a>");
}else{
echo("NEXT".$limit);
}

mysql_free_result($result);

?>

Reply With Quote
  #2 (permalink)  
Old 10-16-2007
Rik Wasmus
 
Posts: n/a
Default Re: Pagination Tutorial

On Tue, 16 Oct 2007 13:40:32 +0200, Alec <ajtdds@aol.com> 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
>
> <?php
> $connection = @mysql_connect('localhost', '*****', '*****');
> if (!$connection) {
> echo '<p>Unable to make database connection.</p>';
> exit();
> }
> if (!@mysql_select_db('uks49179')) {
> exit('<p>Unable to locate database.</p>');
> }
>
> $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);


$totalrows here should become FALSE or 1 according to the code, never,
ever, anything else.

$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_result($result_count,0,0);


> if(empty($page)){


register_globals should be disabled, and is probably the problem. Or have
you filled $page somewhere else?

> $page = 1;
> }
>
> $limitvalue = $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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:56 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0