Jump to content

[SOLVED] gallery next/previous button issues....


NerdConcepts

Recommended Posts

Ok, I've fully built gallery, categories, etc etc.

 

Problem.

 

I run (to select all photos)

$query = mysql_query("SELECT * FROM gallery_photos WHERE cat_id='{$_GET['cat']}'");

 

when someone clicks on a photos it then loads into an ajax piece (which works just fine).

But I am trying to generate a previous and next button. Problem that I am having is that all the categories are dynamic. So cat_id maybe 1,2,3,4,etc. and since the photo information in the database are all stored in the same location "gallery_photos" their photo_id maybe 1,4,28,53, etc. No telling what the ID will be. That is why I cannot just do a simple NEXT button is -1 of the current photo_id. I am not sure how-to do a query where it take the current photo_id then goes back 1 row where the cat_id is the same. That away photos won't display in the wrong category.

 

Hope this makes since.

Link to comment
Share on other sites

When you select all of the results where they're in the category, you can now make an array of all of the values.

It would look like

$photoIDs = array(1,4,28,53);

 

So when you're on the first photo, you're at $photos[0]. next adds one, previous subtracts. Use javascript to keep track of what index you are in the array.

var selected = 0;

//Whatever Js code you are using to register the click

if it's next, selected++

if it's prev, selected--

 

Hope that helps.

Link to comment
Share on other sites

Kind of, but one slight problem. I don't know a lot about javascript. Just what it takes to write the AJax framework. I try to use PHP to do everything. The only reason AJax is being used to display the photos is so the page doesn't have to refresh and the person loose what part of the thumbnails they are viewing. All the thumbnails are displayed in a horizontal scroll DIV tag which has them displayed inside. So if the page reloaded it was start back at the beginning of all the thumbnails...which wouldn't be a good thing. What there is, is a php page that actually does all the photo displaying. The ajax frame is reloaded with a new &photo=x and it takes that x which is the photo_id and display the previous button, photo, then next button. So I was trying to use PHP to take that photo_id, rerun the query but this time figure out where in the whole scheme of things (out of all the photos in the gallery category) and do a -1 and +1 for the previous and back buttons.

Link to comment
Share on other sites

What you said makes no sense. Also, line breaks are good.

 

There are lots of existing ajax photo galleries out there. You could look at what they do.

 

Or, you could do what I said instead of trying to do it with PHP and MySQL, and use the existing data you already have and write the javascript. Check out Mootools. It's a great JS library.

Link to comment
Share on other sites

Ok, just forget about javascript, please. The content of the actual photo display and next/previous buttons are all done with PHP. AJax is only used so that when switching photos, the page doesn't refresh.

 

The page that display the photos (which, yes, is loaded into a AJax container) is as so: photo.php?cat=y&photo_id=x

 

It takes the 'photo_id' and then from there queries it and finds out the photo name and then displays that photo.

 

Since the photo is actually displayed on a totally different page (coding wise) it has to re-run any queries pertaining to that photo.

 

First: photo_id=5

 

$query = mysql_query("SELECT * FROM gallery_photos WHERE photo_id='{$_GET['photo_id']}'");

$row = mysql_fetch_array($query, MYSQL_ASSOC);

echo '<img src="' . $row['photo_full'] . '" alt="" border="0" />';

 

Second: (next and previous buttons)

(query to find out all the photos in that category)

$query = mysql_query("SELECT photo_id FROM gallery_photos WHERE cat_id='{$_GET['cat']}'");

 

what I cannot get to work or really know how is to get all available photos (basically the IDs from the query above). Then find the ID that is it currently on (&photo_id=x) then out of all the photos available go back 1 and return that photo_id for the previous button. And go forward 1 ID to get the next button.

 

Side note:

Only thing that I can figure out is how to put the results into an array. This is how I understand how to do it:

$photoarray = array();

$query = mysql_query("SELECT photo_id FROM gallery_photos WHERE cat_id='{$_GET['cat']}'");

while($row=mysql_fetch_assoc($query) {

  array_push($photoarray, $row);

}

 

not sure how that would work, neither how to find the placement of the currently viewed photo_id and do the -1 +1 things for the buttons.

Link to comment
Share on other sites

If each page only holds one image and the db is searched each time, then as Jesiroe states use the index of an array and code the actual image id into the -1 and +1 links (&photo_id=x-1), then when extract array from db find img_id x and then change to -1 or +1 of the array index and display that image id...

Link to comment
Share on other sites

thats the problem though, I do not know how to find the photo_id inside of the array.

 

The way I can think of doing the linking is using another _GET statement. IE: &photo_id=x&next. Then when it picks up if the next is present it finds the photo_id=x in the array and goes forward one and displays the ID. I'm not sure how to do this. That is the issue. I understand the concept but not how to execute it.

Link to comment
Share on other sites

<a href="?dir=-1&img=555">prev</a> <img src="here.img"> <a href="?dir=1&img=555">next</a>

Try that, then look through the array for the current (last) image and then get the image id for either the one before or after the array position, $a[i-1]

$i = 0;
foreach($a as $e)
{
   if($e == $_GET['img'])
   {
      $newimage = $a[$i + $_GET['dir']];
   }
   $i++;
}

 

By way, this is unbelievably unsafe ($_GET needs checking) and untested, probably change to a for loop, but prolly no great savings. However this should give you a basic idea!

Link to comment
Share on other sites

Thanks for the help, but I do think I am going about this wrong. I'm looking to have physical links to the images, not -1 or 1 in the url to decide which photo to display.

 

Only thing that I can think of it change the way I am going about the _GET functionality. If I do a query when displaying all the thumbnails, put it into an array also, then use the array values ei: $a[0][0], $a[1][0] using the whole $x = 0 and in the while functions do a $x++ thing, so that I can calculate the array offset basically and put that into the link. Then when the photo.php picks up the photo=x it'll recreate the array and get the "real" photo_id and then display it. That is my only guess and I'm almost possitive on how to do that.

 

But without all this support I wouldn't have thought of that...so I have to thank you. Plus your code did fix another problem that I was having on another "play" site I've been working on. Thanks you two.

Link to comment
Share on other sites

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.