Log in

View Full Version : How to use a Next & Previous button in an array



kuau
08-12-2010, 01:26 AM
I am really exhausted so please forgive me if this is a really dumb, obvious question.

I am displaying one artist's images one per page taken from a database table that contains images for more than one artist. I made a Previous and Next button by adding or subtracting 1 from the title_id. The problem is that it also displays other artists' images because they are in the same table.

How do I add 1 to the array created by the SELECT clause instead of the title_id so that it will display images for only the one artist? Or is there a better way to do Previous and Next buttons?

Thanks. :)

bluewalrus
08-12-2010, 02:01 AM
So your db columns are Image, and title_id, are there any others?

kuau
08-12-2010, 02:36 AM
Oh, sorry, I was unclear. The SELECT clause just gets the title_id, title, filename, and image dimensions from the database WHERE artist_id = '1'. The images are not stored in the table. The images are just jpegs in a folder.

bluewalrus
08-12-2010, 03:14 AM
I'd do 2 selects one to get the top three ids. I'd use these ids as links first id as the previous, second as current, and third as next. This is just an idea not sure if correct or will work. I'd also have the previousID passed over.



Select top 3 title_id from images WHERE artist_id = '1' and title_id <= $previous ID

Select title_id, title, filename, dimensions from images WHERE artist_id = '1' and title_id = $one or title_id = $two or title_id = $three

james438
08-12-2010, 05:10 AM
SELECT ID FROM image_table WHERE title_id < $title_id and artist_id = '1' limit 1

$title_id is the current ID of the image. The above query will give you the previous row in your database which has the same artist_id value. This will give you the data you need for your "prev" link/button.


I made a Previous and Next button by adding or subtracting 1 from the title_id.
common mistake. I recently corrected a few of my "prev" and "next" links where I did the same thing you mentioned in your first post. By doing what you quoted you will get the previous value in your database which may or may not have the correct artist.

kuau
05-09-2011, 11:33 PM
Is there any way to use the php next and prev commands to create these buttons?

I tried to create an array and set the current value to the current title_id but it kept coming out as '1'. How do you set the pointer to start at a specific value in the array? I want to display the next and prev images based on the titles sorted alphabetically. The ids are not in order. I thought if the array were sorted by title, the next and prev commands would do what I wanted, but it didn't work. Any ideas?


$sql4 = "SELECT title_id, title FROM `title` WHERE `artist_id` = $artist_id AND `on` = 1 ORDER BY `title`";
$result4 = mysql_query($sql4,$connection) or die("Couldn't execute $sql4 query. <br> mysql error: ".mysql_error());
$row = mysql_fetch_array($result4);
$art['title_id'] = current($row);

This is the code snippet that is behind the Previous button... which doesn't work.

?title_id=<?php echo prev($row);?>

Thanks for any help.

midhul
05-10-2011, 05:17 AM
I Think the thing you need is a bit similar to this: http://dynamicdrive.com/forums/showthread.php?t=62234

Without the update script

kuau
05-10-2011, 11:02 AM
I did check that other thread (thank you) and I can see how it would work if you start at the beginning of the array, but people come to my page by clicking arbitrarily on one of 190 thumbnails so they could start at image 185. Maybe I'm missing something but I still don't see how to start at a specific place within the array. Is there some command that tells you where you are in the sorted array? ie. you are at the 185th row so current ($array) = 185 and next($array) = 186 and prev($array) = 184

midhul
05-11-2011, 06:49 AM
Well, the script that I posted there does work even when started from any value. (Thats the reason, I didn't loop, to allow input from GET)

So doing ?i=2, will also work. So if it's customized for your case, that thumbnail should link to ______.php/?i=185