dan2684 Posted July 15, 2008 Share Posted July 15, 2008 Hi there, I'm trying to build an image gallery and it's proving to be more problematic that I originally thought! Here's what I want to do: - Thumbnails - When you click a thumbnail you it takes you to the larger picture with a "next" and a "previous" button - First image goes to the last image when "previous" is clicked - Last image goes to the first image when "next" is clicked I've managed to do get this working by passing a number of variables through the url each time - The total amount of images, the ID of the first image, and so on... The problem! If you delete an image, it messes up the "next" and "previous" thing because it works by incrementing the imageID, and mySql leaves a 'gap' in the table. Any help would be greatly appreciated Cheers, Dan Quote Link to comment https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/ Share on other sites More sharing options...
aim25 Posted July 15, 2008 Share Posted July 15, 2008 Have an IF statement that checks if the photo ID exists before you output the photo. Quote Link to comment https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/#findComment-590991 Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 instead of doing next by adding 1 to the number.. you could try the following please note this was written on the fly and probably had a ton of bugs, but i hope it gives you the idea <?php $start = (int)$_GET['img']; $sql = "SELECT * FROM imagetable LIMIT ".($start-1).", 3"; //this get 3 images $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } $n = 0; while ($row = mysql_fetch_assoc($result)) { $n++; switch($n) { case 1; echo "<a href=\"?img={$row['id']}\">prev</a>"; break; case 2; echo "<img src=\"{$row['imgpath']}\"><br><a href=\"?img={$row['id']}\">Full</a>"; break; case 3; echo "<a href=\"?img={$row['id']}\">Next</a>"; break; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/#findComment-590994 Share on other sites More sharing options...
dan2684 Posted July 15, 2008 Author Share Posted July 15, 2008 Is it a problem going backwards and forwards from the database like that? Quote Link to comment https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/#findComment-590995 Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 not really.. infact read up on "php pagination".. should give you a better understanding Quote Link to comment https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/#findComment-590999 Share on other sites More sharing options...
dan2684 Posted July 15, 2008 Author Share Posted July 15, 2008 Sorry - That last comment wasn't to MadTechie, it was for Aim25! Quote Link to comment https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/#findComment-591000 Share on other sites More sharing options...
dannyb785 Posted July 15, 2008 Share Posted July 15, 2008 Is it a problem going backwards and forwards from the database like that? it's not bad, just probably uses more processing memory than needed. bc if one image is id #2 and the next is id#30, you have to check 28 images before you find one. Even though computers are pretty fast, kimagine if you have a few hundred(or thousand) users doing it at the same time. Not efficient! An easy method you could do would be for the previous image, SELECT * FROM Image WHERE image_id < '$current_image_id' LIMIT 1 for the the next image, SELECT * FROM Image WHERE image_id > '$current_image_id' LIMIT 1 where $current_image_id is the image currently being viewed(if that wasn't obvious). Quote Link to comment https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/#findComment-591035 Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 for the previous image, SELECT * FROM Image WHERE image_id < '$current_image_id' LIMIT 1 for the the next image, SELECT * FROM Image WHERE image_id > '$current_image_id' LIMIT 1 that won't work.. okay current image is 5 i have 10 images (from 1 to 10) using the above selects next would be 6 BUT prevous would be 1. as i suggested before do some reseach on pagination, here some info http://www.tonymarston.net/php-mysql/pagination.html you could also search this forum.. when it comes to $rows_per_page just use 1 Quote Link to comment https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/#findComment-591060 Share on other sites More sharing options...
dan2684 Posted July 15, 2008 Author Share Posted July 15, 2008 I've managed to do it! MadTechie - I'm reading up on pagination as we speak ;-) Really appreciate your help. Loving this PHP stuff! Thank you thank you thank you Quote Link to comment https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/#findComment-591095 Share on other sites More sharing options...
dannyb785 Posted July 16, 2008 Share Posted July 16, 2008 for the previous image, SELECT * FROM Image WHERE image_id < '$current_image_id' LIMIT 1 for the the next image, SELECT * FROM Image WHERE image_id > '$current_image_id' LIMIT 1 that won't work.. okay current image is 5 i have 10 images (from 1 to 10) using the above selects next would be 6 BUT prevous would be 1. as i suggested before do some reseach on pagination, here some info http://www.tonymarston.net/php-mysql/pagination.html you could also search this forum.. when it comes to $rows_per_page just use 1 My bad, for the 'previous' link, you'd need to order by image_id DESC. Then it'd be fine. MadTechie - I'm reading up on pagination as we speak ;-) Really appreciate your help. Loving this PHP stuff! good to know you didn't even acknowledge my help. Quote Link to comment https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/#findComment-591298 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.