I use this (probably quite badly coded, but hey, i was in a rush!!) code to do a photograph display thing that is quite similar to what you want.
<?php
$current_dir = "photos";
$dir = opendir($current_dir);
$number_of_photos = 0;
while($file=readdir($dir))
{
$number_of_photos++;
}
$number_of_photos -= 2;
echo "<p>";
if((int)$photo>10)
{
(int)$previous_photo=((int)$photo-1);
echo "<a href=\"display_photo.php?photo=".(int)$previous_photo.".jpg\">Previous Photo</a> | ";
if((int)$photo==($number_of_photos-1))
echo "<a href=\"display_photo.php?photo=00.jpg\">Next Photo</a>";
else
{
$next_photo=($photo+1);
echo "<a href=\"display_photo.php?photo=".(int)$next_photo.".jpg\">Next Photo</a>";
}
}
else
{
(int)$previous_photo=((int)$photo-1);
if((int)$previous_photo>=0)
echo "<a href=\"display_photo.php?photo=0".(int)$previous_photo.".jpg\">Previous Photo</a> | ";
else
echo "<a href=\"display_photo.php?photo=".(int)($number_of_photos-1).".jpg\">Previous Photo</a> | ";
if((int)$photo==($number_of_photos-1))
$next_photo=00;
else
$next_photo=($photo+1);
if((int)$photo>8)
echo "<a href=\"display_photo.php?photo=".(int)$next_photo.".jpg\">Next Photo</a>";
else
echo "<a href=\"display_photo.php?photo=0".(int)$next_photo.".jpg\">Next Photo</a>";
}
?>
There's nothing too complicated in there - just have a look through and see if you can get what's gonig on and the tinker with it to do what you want yours to do.