Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:58:41+00:00 2026-06-13T01:58:41+00:00

I have a comics website which loops through all images in a db and

  • 0

I have a comics website which loops through all images in a db and displays them as thumbnails.
The user can click on one of those images to see it in normal size on a viewComic.php template.

I’d like to allow users to press left and right arrows to navigate images.

So, my idea is:

  1. pagination.php handles image display on correct pages (by offsetting) by looping through database result array. The user can click on a result (below) to go to that specific image on the viewcomic.php template.

    '<br />IMG: <a href="./templates/viewcomic.php?id=' . $row['imgid'] . '&image=' . $imgpath.$row['imgname'] . '">
    
  2. Now on viewcomic.php, I get and display the image

    $imgid = $_GET['id']; 
    $imgpath = $_GET['image']; 
    <center><img src=".<?php echo  $imgpath  ?>" /></center>
    

The user can press left and right arrows to navigate through images…

Question 1: Does Javascript allow you to manually set the index position in an array?

Since I want to allow the user to get next and last images, I want them to start from the image they’re currently on (the one they clicked on in pagination.php), not from the beginning of the image array. I pass along the $imgid in the URL, so I was going to set the javascript img array index == $imgid, which would allow the user to continue scrolling through the images from where they left off.

Question 2: How can I keep the current index of an array?

When I want to go a certain direction, it works fine. But if I want to change directions, I have to press the key twice. I found this is because the index is incremented after (imgIndex++) I’ve pressed the key. Meaning, that when it starts to go the opposite way, it first needs to decrement to return the index to the current image, and then press the key again to finally show the next image. I’m not sure how to fix this. Using ++imgIndex is also problematic because it will skip forward an image.

<?php
 getImages() {
//DB connection code omitted
   $img = array();
   while($row = $catResult->fetch_assoc()) {
       $img[] = "'../images/all_comics/" . $row['imgname'] . "'";
}
return $img;
?>

var imgArray = [<?php echo implode(',', getImages()) ?>];
$(document).ready(function() {
    var img = document.getElementById("theImage");
    img.src = imgArray[0];
    var imgIndex = 0;

    $(document).keydown(function (e) 
    {
        //next image
        if (e.which == 39) 
        {
            if (imgIndex == imgArray.length) 
            {
                imgIndex = 0;
            }
            img.src = imgArray[imgIndex++];
                    //now the index is ahead by 1, which is bad if you want to press <-- to get the last image because the index is ahead by 1.
        }

        //last image
        if (e.which == 37) 
        {
            if (imgIndex == 0) 
            {
                imgIndex = imgArray.length;
            }
            img.src = imgArray[--imgIndex];
        }
    });
}); 

Any ideas?

Much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:58:42+00:00Added an answer on June 13, 2026 at 1:58 am

    Change the order of operations:

    if (e.which == 39) {
        imgIndex++;            
        if (imgIndex>imgArray.length)  {
            imgIndex = 0;
        }
        img.src = imgArray[imgIndex];                
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comics website which loops through all images in a db and
Have deployed numerous report parts which reference the same view however one of them
I have written a Python script to download all of the xkcd comic images.
I'm building a website which will show comics. I'd like to store each image
I have a simple application that views comics and allows the user to mark
I have these 2 queries and i would like to join them into one
Comics object can have many Chapter object. I have this in Comics class: @OneToMany(targetEntity=Chapter.class,
I have a website on which I want to post the current xkcd comic.
Inspired by the XKCD geohashing comic (http://imgs.xkcd.com/comics/geohashing.png), I thought I'd have a go at
have written this little class, which generates a UUID every time an object of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.