Forum Moderators: coopster

Message Too Old, No Replies

Problem with pagination. my next and previous button always show!

         

aludo325

1:03 am on Jan 19, 2009 (gmt 0)

10+ Year Member



So I got everything working including the previous button, but I can't figure out how to get the next button to only come up when there is supposed to be another page. Since the previous button comes up whenever the next button comes up, they keep coming up even when I have no more search results. Please take a look...

<?
$user="*****";
$password="*****";
$database="*****";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
?>

<?
$link = mysql_connect("*****", "*****", "*****");
mysql_select_db("*****", $link);
?>

<?

$pagemove = 1;
$increment = 8;

$page = (int) $_GET['page'];
$page = (isset($_GET['page'])) ? $page : 1;

$result = mysql_query("SELECT * FROM countries WHERE country_category='europe'", $link);
$num_rows = mysql_num_rows($result);

$mull = ($num_rows - 8) * $page;

$query="SELECT * FROM country WHERE country_category='europe' LIMIT ";
$query .= $page * $increment - $increment . ", " . $increment;

$result = mysql_query($query);

mysql_close();

echo "<b><center><br />European Countries</center></b><br><br>";

while ($row = mysql_fetch_array($result))
{
echo '<a href="' . $row['country_link'] . '"target="_blank">' . $row['country_name'] . '<br /><br /></a><br /> ';
}

if ($page > 1) {
$prev = $page - $pagemove;
$url = $_SERVER['PHP_SELF']."?page=$prev";
printf("<a href='%s'><br /><b>Previous Page</b></a>", $url);
}

if ($num_rows > ($increment + $page)) {
$next = $page + $pagemove;
$url = $_SERVER['PHP_SELF']."?page=$next";
printf("<a href='%s'><br /><b>Next Page</b></a>", $url);
}

?>

Thanks..

supermanjnk

3:12 pm on Jan 19, 2009 (gmt 0)

10+ Year Member



if ($num_rows > ($increment + $page)) {
$next = $page + $pagemove;
$url = $_SERVER['PHP_SELF']."?page=$next";
printf("<a href='%s'><br /><b>Next Page</b></a>", $url);
}

change $increment + $page to $increment * $page

If you have 40 results you should have 5 pages
in this case $num_rows = 40, $increment = 8 and $page will = 1, so you show the next button if $num_rows is greater then 9, your next page will do 8 + 2 (page 2) and thus be checking if 40 is greater then 10. The way it current is shown you should lose the next button at about page 32.