Results 1 to 6 of 6

Thread: [RESOLVED]Getting the previous and next Mysql entries

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Resolved [RESOLVED]Getting the previous and next Mysql entries

    I am storing paths to images in a db, what I want to do is get an image path from the db, but then also get the previous entry and next entry around the current path that I selected. I am making a photo gallery and want to display 2 thumbnails for navigation on each side of the "main" bigger picture.

    Any help would be great
    Last edited by zalez; May 18th, 2006 at 01:10 AM.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Getting the previous and next Mysql entries

    How are you retrieving the data? If you could post the code you are using to do that we can suggest something.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Getting the previous and next Mysql entries

    What's your concept of "previous" and "next"? The DB itself doesn't really have one - it depends on the sorting of the data.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Re: Getting the previous and next Mysql entries

    Basically i'll pass the photo id in the url, then I have a function that gets the id and queries the db for that id.

    This is the type of url I am using:
    photo.php?imageid=9

    PHP Code:
    function display_image() {
    if(isset(
    $_GET['imageid'])){
    $imageid $_GET['imageid'];
    $result mysql_query("SELECT * FROM photos WHERE id = '$imageid'");
    $myrow mysql_fetch_array($result);
    $uid $myrow[userid];
    $imagename $myrow[fname] . "." $myrow[ext];
    $uresult mysql_query("SELECT * FROM users WHERE userid = '$uid'");
    $umyrow mysql_fetch_array($uresult);
    $path "/photo/upload_pic/$umyrow[username]/$imagename";
    echo 
    $path;
      }

    I would just use the photo id and subtract one or add one but im giving the user the option to delete photos so they will not always be sequencial.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Getting the previous and next Mysql entries

    PHP Code:
    $imageid intval($_GET['imageid']); // Prevent SQL injection.
    // Query for the current row:
    $query "SELECT * FROM photos WHERE id = $imageid";
    // For the previous:
    $query "SELECT * FROM photos WHERE id < $imageid ORDER BY id DESC LIMIT 1";
    // For the next:
    $query "SELECT * FROM photos WHERE id > $imageid ORDER BY id ASC LIMIT 1"
    Last edited by john tindell; May 17th, 2006 at 07:21 AM. Reason: Fixed Tags
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Re: Getting the previous and next Mysql entries

    Something so simple yet so far away from my brain Thanks alot Cornedbee, it worked like a charm.

Posting Permissions

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



Click Here to Expand Forum to Full Width