Results 1 to 9 of 9

Thread: How to use a Next & Previous button in an array

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default How to use a Next & Previous button in an array

    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.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    So your db columns are Image, and title_id, are there any others?
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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.

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.

    Code:
    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
    Corrections to my coding/thoughts welcome.

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Code:
    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.
    Last edited by james438; 08-12-2010 at 07:19 AM. Reason: slight rewording.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #6
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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?

    Code:
    $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.
    Code:
    ?title_id=<?php echo prev($row);?>
    Thanks for any help.
    Last edited by kuau; 05-09-2011 at 11:34 PM. Reason: sp

  7. #7
    Join Date
    Feb 2008
    Posts
    81
    Thanks
    8
    Thanked 5 Times in 5 Posts

    Default

    I Think the thing you need is a bit similar to this: http://dynamicdrive.com/forums/showthread.php?t=62234

    Without the update script

  8. #8
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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

  9. #9
    Join Date
    Feb 2008
    Posts
    81
    Thanks
    8
    Thanked 5 Times in 5 Posts

    Default

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •