How to make Next and Prev buttons to work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slsv
    New Member
    • Apr 2013
    • 14

    How to make Next and Prev buttons to work

    Hi,
    still learning and I trying to make buttons for Next and Prev.
    So far I can pull the image from database and show in the page but buttons doesn't work. When I click on Next or Prev it's just reload the page with the same image.
    Can someone help me with this?
    Here is the code
    Code:
                if($q = mysqli_query($con, "SELECT * FROM images WHERE `name` = '". preg_replace('/[^.A-Za-z0-9]/ui', '',  $_GET['name']) ."' LIMIT 1")) {
                    if($row = mysqli_fetch_array($q)){
                        echo '<a href="pic.php?name='.$row['name'].'" class="button button-blue"><span>Prev</span></a>';
                        echo '<a href="pic.php?name='.$row['name'].'" class="button_next button-blue"><span>Next</span> </a>';
                        echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$row['name']."\" />";
    
                    } else {
                        echo 'Not found';
                    }
                } else {
                    echo mysqli_error($con);
                }
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    All you are doing there is creating Next and Prev links with the name of the image you are currently on. What you need to do is link to the next or previous images in the list.

    Generally, you want to link to things like this using an ID or an index; a number that you can add or subtract from. That makes it far easier to create the links. All you'd have to do is add or subtract one from the current ID/index. - If you want to use a name, you will need to fetch the names of the next and previous images from the database and put those in the link. That would require at least one other query, to fetch those.

    Consider that you can use queries like this one to find the name of the previous image, based on the name of the current image:
    Code:
    SELECT name FROM images
    WHERE id < (
        SELECT id FROM images WHERE name = 'current name'
    )
    ORDER BY id DESC
    LIMIT 1;
    Then you can just flip the where condition and the order by orientation to get the next image.

    Comment

    • slsv
      New Member
      • Apr 2013
      • 14

      #3
      So I need 3 query's?
      1. To select all images from table
      2. To select prev image on Prev button
      3. To select next image on Next button

      Or I didn't understand something?

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Yes, if you want to go with the name approach, you will have to fetch all three names out of the database. - Although, you could probably combine the three queries into one, if you understand subqueries and/or joins.

        If you use an index in your GET parameter, instead of (or with) the name, then you'd only need one query. Consider:
        Code:
        SELECT name FROM images
        LIMIT 1 OFFSET x
        Where [il]x[/il] represents the index of the image you want to fetch.

        Comment

        Working...