Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/114912-solved-php-image-gallery/
Share on other sites

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;
}
}
?>

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).

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.