...

View Full Version : Next-Previous function needs help



zonkd
09-20-2005, 02:58 PM
This Next | Previous function works well, but for the last page, I need to link back to the start.

I’m trying to say in this code that if the end is reached – id 45 – then return to the start page. But it doesn’t want to work like this. Advice would be appreciated, experts.

$next = $_GET['id'] + 1;
$previous = $_GET['id'] - 1;
$total = 45;

if ($previous > -1)
{
echo '<p><a href="?id=' . $previous . '">Previous</a> | ';
}
if ($next <= $total)
{
echo '<a href="?id=' . $next . '">Next</a><br>';
}

if(!$id > 44)
{ echo '<a href="Thoughts.php" Title="Back to the Philosophy titles."> Back to the start</a>'
}
?>

NancyJ
09-20-2005, 03:07 PM
Do you have a link where we can see this working?
What is happening?

macmonkey
09-20-2005, 11:11 PM
$next = $_GET['id'] + 1;
$previous = $_GET['id'] - 1;
$total = 45;

if ($previous >= 0)
{
echo '<p><a href=\"thispage.php?id=$previous\">Previous</a> | ';
}
if ($next <= $total)
{
echo '<a href="\"thispage.php?id=$next\">Next</a><br>';
}

if($id > 44)
{ echo '<a href="Thoughts.php" Title="Back to the Philosophy titles."> Back to the start</a>'
}
?>

Hope it helps.

zonkd
09-21-2005, 11:06 AM
Many thanks macmonkey and NancyJ.

macmonkey, it doesn't want to work out, not even when I state the last id and say if it equals that number, show this page.

I'll have to have more of a fiddle later.

Good to hear from you NancyJ. Unfortunately, the pages aren't on a server yet. I think part of the trouble would be solved if I could renumber the records in chronological order. But I don't think that's possible - or if it is, be very pleased to hear how.

Cheers, Experts.

NancyJ
09-21-2005, 11:11 AM
Shouldnt this:

if(!$id > 44)
{ echo '<a href="Thoughts.php" Title="Back to the Philosophy titles."> Back to the start</a>'
}
be

if($id > 44)
{ echo '<a href="Thoughts.php" Title="Back to the Philosophy titles."> Back to the start</a>'
}

If I'm understanding you correctly you want to show the 'Back to Start' link when $id is greater than 44 (ie when its 45) but your code says show the link when $id is NOT greater than 44...
is this what is happening?

If this isnt what is wrong it would be helpful for you to tell us what exactly is happening, since we cant experience it for ourselves we have to rely on you to describe the problem... just saying it doesnt work like you want isnt enough information

zonkd
09-21-2005, 02:13 PM
Dear NancyJ

Thanks very much. Of course, and such a simple error. Thank you very much for finding it, and telling me. Insanity averted, thank you.

macmonkey
09-21-2005, 03:24 PM
since I can't see your full query I'll do an example of how I'd do it. It may be 1/2 assed but works for me:


<?
include('includes/db_connect.php'); // connect to mysql db

$result = mysql_query("SELECT * FROM stories");
while($myrow = mysql_fetch_assoc($result)) {
$next = $myrow['id'] + 1;
$previous = $myrow['id'] - 1;
$total = 45;

if ($previous >= 0) {
echo "<p><a href=\"thispage.php?id=$previous\">Previous</a> | ";
}
if ($next <= $total)
{
echo "<a href=\"thispage.php?id=$next\">Next</a><br>";
}

if($id > 44)
{ echo "<a href=\"Thoughts.php\" Title=\"Back to the Philosophy titles.\"> Back to the start</a>";
}
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum