gfX Posted November 25, 2006 Share Posted November 25, 2006 Hey guys,I have a gallery for a website: http://www.vivagaete.com/gallery.phpIf you click on the thumbnail it will bring up viewgallery.php - which has the larger image on it.What I want, is for viewgallery.php to have the options for "PREVIOUS and NEXT" images. So they don't have to click "Click here to Go Back" every time, they can browse by clicking the NEXT link or PREVIOUS.How would I go about doing this to pull the next image ID from the database?Here is the code for viewgallery.php:[code]<style type="text/css">.greentext { color: #2a8f3d; margin: 4px; font-family: Tahoma; font-size: 19px;}.blacktext { color: #000000; margin: 4px; font-family: Tahoma; font-size: 11px;}</style><?include 'db.php';$getid = $_GET['id'];$sql = "SELECT * FROM gallery WHERE id = $getid";$result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { $picture = $row['picture']; $heading = $row['heading']; $description = $row['description'];}$sql2 = "SELECT id FROM gallery WHERE id > $id LIMIT 1";$result2 = mysql_query($sql2); while ($row = mysql_fetch_array($result)) { $id = $row['id'];}if ($getid == "$id") { echo "<center> <FORM> $id<INPUT type='button' value='Click here to go back' onClick='history.back()'></FORM><div class=\"greentext\"><b>$heading</b></div><div class=\"blacktext\">$description</div><br /><img src=\"images/$picture\" style=\"border: 2px solid #d4cccc;\"><br /><br /><FORM><INPUT type='button' value='Click here to go back' onClick='history.back()'></FORM></center>";} else {echo "No image verfied";}?>[/code]Thanks a lot!! Quote Link to comment Share on other sites More sharing options...
mansuang Posted November 25, 2006 Share Posted November 25, 2006 The easiest way:[code=php:0]<a href="<?=$_SERVER['PHP_SELF']; ?>?id=<?=((int)$_GET['id']-1); ?>">Previous</a><a href="<?=$_SERVER['PHP_SELF']; ?>?id=<?=((int)$_GET['id']+1); ?>">Next</a>[/code] Quote Link to comment Share on other sites More sharing options...
Philip Posted November 25, 2006 Share Posted November 25, 2006 However with the post above, you will want to have an if statement checking to see if there is that photo in the DB.Ex: click on it more than once and you could get id=-1 Quote Link to comment Share on other sites More sharing options...
gfX Posted November 25, 2006 Author Share Posted November 25, 2006 Thanks for the replies! That did work, but like KingPhilip said, I need it to check if it is in the database to get the right ID's.How would I make that if statement?Thanks a lot for the help. Quote Link to comment Share on other sites More sharing options...
Philip Posted November 25, 2006 Share Posted November 25, 2006 This should work (it has been a while since I've done some coding, so let me know if I made a mistake somewhere)[code]<style type="text/css">.greentext { color: #2a8f3d; margin: 4px; font-family: Tahoma; font-size: 19px;}.blacktext { color: #000000; margin: 4px; font-family: Tahoma; font-size: 11px;}</style><?phpinclude 'db.php';$getid = $_GET['id'];$sql = "SELECT * FROM gallery WHERE id = $getid";$result = mysql_query($sql);$total_rows = mysql_num_rows($sql); // find out how many rows are in that table, see what the max number iswhile ($row = mysql_fetch_array($result)) { $picture = $row['picture']; $heading = $row['heading']; $description = $row['description'];}$sql2 = "SELECT id FROM gallery WHERE id > $id LIMIT 1";$result2 = mysql_query($sql2);while ($row = mysql_fetch_array($result)) { $id = $row['id'];}if ($getid == "$id") { echo "<div align='center'> <div class=\"greentext\"><b>$heading</b></div> <div class=\"blacktext\">$description</div><br /> <img src=\"images/$picture\" style=\"border: 2px solid #d4cccc;\"><br /><br />"; if(($getid-1) <= 0) { // if id in link is less than or equal to 0 echo "Previous Image"; // show just text } else { $id_prev = $getid-1; //otherwise, set variable echo "<a href='/viewgallery.php?id=".$id_prev."'>Previous Image</a>"; // and show a link to previous image } echo " - "; // spacer between links if(($getid+1) > $total_rows) { //if the id in link is more than total rows echo "Next Image"; //show just normal text.. no link } else { $id_next = $getid+1; // otherwise, set variable echo "<a href='/viewgallery.php?id=".$id_next."'>Next Image</a>"; //and provide a link to next image } echo "</div>"; // end center} else { echo "No image verfied";}?>[/code][i]edited to make it easier to read the code - ACK I forgot a semicolon.. sorry![/i] Quote Link to comment Share on other sites More sharing options...
gfX Posted November 25, 2006 Author Share Posted November 25, 2006 Hey there,Thakns for taking the time to supply that code!I get: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /hsphere/local/home2/andres30/vivagaete.com/viewgallery.php on line 21Line 21: $total_rows = mysql_num_rows($sql); // find out how many rows are in that table, see what the max number ishttp://www.vivagaete.com/viewgallery.php?id=5 Quote Link to comment Share on other sites More sharing options...
Philip Posted November 25, 2006 Share Posted November 25, 2006 Whoops, change those lines (19-21) to this:[code]<?php$sql = "SELECT * FROM gallery WHERE id = $getid";$result = mysql_query($sql);$total_rows = mysql_num_rows($result); // find out how many rows are in that table, see what the max number is?>[/code] Quote Link to comment Share on other sites More sharing options...
gfX Posted November 25, 2006 Author Share Posted November 25, 2006 Great man, fixed that one. Now the only problem is that there is no next link. It just shows the text.Any reason for that? Quote Link to comment Share on other sites More sharing options...
Philip Posted November 25, 2006 Share Posted November 25, 2006 Try:[code]<style type="text/css">.greentext { color: #2a8f3d; margin: 4px; font-family: Tahoma; font-size: 19px;}.blacktext { color: #000000; margin: 4px; font-family: Tahoma; font-size: 11px;}</style><?phpinclude 'db.php';$getid = $_GET['id'];$sql = "SELECT * FROM gallery WHERE id = $getid";$result = mysql_query($sql);$total_rows = mysql_num_rows(mysql_query("SELECT * FROM gallery")); // find out how many rows are in that table, see what the max number iswhile ($row = mysql_fetch_array($result)) { $picture = $row['picture']; $heading = $row['heading']; $description = $row['description'];}$sql2 = "SELECT id FROM gallery WHERE id > $id LIMIT 1";$result2 = mysql_query($sql2);while ($row = mysql_fetch_array($result)) { $id = $row['id'];}if ($getid == $id) { echo "<div align='center'> <div class=\"greentext\"><b>$heading</b></div> <div class=\"blacktext\">$description</div><br /> <img src=\"images/$picture\" style=\"border: 2px solid #d4cccc;\"><br /><br />"; if(($getid-1) <= 0) { // if id in link is less than or equal to 0 echo "Previous Image"; // show just text } else { $id_prev = $getid-1; //otherwise, set variable echo "<a href='/viewgallery.php?id=".$id_prev."'>Previous Image</a>"; // and show a link to previous image } echo " - "; // spacer between links if(($getid+1) == $total_rows) { //if the id in link is more than total rows echo "Next Image"; //show just normal text.. no link } else { $id_next = $getid+1; // otherwise, set variable echo "<a href='/viewgallery.php?id=".$id_next."'>Next Image</a>"; //and provide a link to next image } echo "</div>"; // end center} else { echo "No image verfied";}?>[/code]Also, I just noticed you're not always having a numerical order - like you start at 5, there isn't a 15, etc.Which, my code will work, but you're going to be cutting off a few images at the end and it will show the images that aren't there -- I recommend just making your table be numerical order. Or, if somebody else would like to post some code showing how to do it another way ;) Quote Link to comment Share on other sites More sharing options...
gfX Posted November 25, 2006 Author Share Posted November 25, 2006 Ah Thanks, that worked.Yeah that's from images being deleted from the database :(I wish that could be fixed, but other than that I really appreciate it!Thakns so much Quote Link to comment Share on other sites More sharing options...
Philip Posted November 25, 2006 Share Posted November 25, 2006 Not a problem :D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.