I've been struggling to get some previous and next buttons to work recently. I finally cracked it though.
I was looking for a simple solution - and there is always a simple solution - but was bombarded by long complex (at least for me) code involving counting and limit and god knows what else.
I'm going to share my code with you guys so that others don't have to suffer the same problems as me!
<?php
include("header.inc");
$db = "gallery";
$link = mysql_connect( "user", "pass" );
if ( !$link ) die( "Couldn't connect to MySQL" );
mysql_select_db( $db, $link ) or die( "Couldn't open $db: ".mysql_error() );
$sqlnext = "SELECT * FROM galleryTable Where tour = '$page' AND id < '$id' ORDER BY id DESC";
$sqlprev = "SELECT * FROM galleryTable Where tour = '$page' AND id > '$id' ";
$resultnext = mysql_query( $sqlnext, $link );
$resultprev = mysql_query( $sqlprev, $link );
?>
<p> </p>
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="10%" valign="middle" class="subtitle"><div align="center"></div></td>
<td width="120%" align=center><?php print "<img src=\"/images/archive/$fupload\">"; ?></td>
<td width="10%" valign="middle" class="subtitle"><div align="center"></div></td>
</tr>
<tr>
<td width="10%"> </td>
<td width="80%"><table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="50%" valign="top">Tour:</td>
<td width="50%"><?php print " $page "; ?></td>
</tr>
<tr>
<td width="50%" valign="top">Name:</td>
<td width="50%"><?php print " $name "; ?></td>
</tr>
<tr>
<td width="50%" valign="top">Materials:</td>
<td width="50%"><?php print " $materials "; ?></td>
</tr>
<tr>
<td width="50%" valign="top">Size:</td>
<td width="50%"><?php $size = stripslashes($size); print " $size "; ?></td>
</tr>
<tr>
<td width="50%" valign="top">Status:</td>
<td width="50%"><?php print " $status "; ?></td>
</tr>
<tr>
<td width="50%" align="center" class="subtitle">
<?php
while ( $newArray = mysql_fetch_array($resultprev) )
{
$id = $newArray['id'];
$status = $newArray['status'];
$name = $newArray['name'];
$materials = $newArray['materials'];
$size = $newArray['size'];
$size = htmlspecialchars($size);
$fupload = $newArray['fupload'];
$tupload = $newArray['tupload'];
print "
<a href=\"display.php?id=$id&fupload=$fupload&name=$name&materials=$materials&status=$status&size=$size&page=$page\">previous</a>
</td>
";
break;
}
while ( $newArray = mysql_fetch_array($resultnext) )
{
$id = $newArray['id'];
$status = $newArray['status'];
$name = $newArray['name'];
$materials = $newArray['materials'];
$size = $newArray['size'];
$size = htmlspecialchars($size);
$fupload = $newArray['fupload'];
$tupload = $newArray['tupload'];
print "
<td width=\"50%\" align=center class=subtitle>
<a href=\"display.php?id=$id&fupload=$fupload&name=$name&materials=$materials&status=$status&size=$size&page=$page\">next</a>
";
break;
}
mysql_close( $link );
?>
</td>
</tr>
</table></td>
<td width="10%"> </td>
</tr>
</table>
<p> </p>
<?php
include("footer.inc");
?>
I set up two arrays and two mysql queries for each array. depending if you press next or prev the next or previous record displays. Also, if there isn't another record then the next or previous button won't display.
This was so easy by comparison to the long scripts I've been reading!