Jump to content

Iterating through an array of images


foucquet
Go to solution Solved by foucquet,

Recommended Posts

I am trying to iterate through an array of images and am having trouble with next/prev links. I thought that this should basically work as a starting point:-

$images = array();
$img_loc = 'images/';

if ($handle = opendir($img_loc)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $images[] = $file;
        }
    }
    closedir($handle);
}

//>>> ====================================================================================
//>>> Block of code for Next/Prev links on images


if (isset($_REQUEST['id'])) {
    $id = $_REQUEST['id'];
    }
    else {
        $id = 0; // Whatever the default starting value should be
    }

$self = $_SERVER['PHP_SELF'] . "?$id=";

$first = 0; // Because arrays are numbered from 0
$last = 7;

$pic = $img_loc . $images[$id];

echo "<p><img src=\"$pic\"></p>";

if ($id < $last) {
    echo "<a href=".$self.($id + 1)."><h3>Next</h3></a>";
    }

if ($id > $first) {
    echo "<a href=".$self.($id - 1)."><h3>Previous</h3></a>";
    }

//=====================================================================================<<<

//>>> ====================================================================================
//>>>  Checking on what the variables are doing

var_dump ($images);

echo "File location = " . $pic . "<br>";
echo "First = " . $first . "<br>";
echo "Last = " . $last . "<br>";
echo "File id = " . $id . "<br>";
echo "Self = " . $self . "<br>";
echo "Server = " . $_SERVER['PHP_SELF'] . "<br>";

//=====================================================================================<<<

and to an extent it does giving me:-

IMAGE

Next
 

array (size = 8 )
  0 => string 'decisions.jpg' (length=13)
  1 => string 'dual_concentration.jpg' (length=22)
  2 => string 'generation_gap.jpg' (length=18)
  3 => string 'just_looking.jpg' (length=16)
  4 => string 'mid_stride.jpg' (length=14)
  5 => string 'no7.jpg' (length=7)
  6 => string 'plugged.jpg' (length=11)
  7 => string 'runner.jpg' (length=10)

File location = images/decisions.jpg
First = 0
Last = 7
File id = 0
Self = /imageanation/next_prev_link_test.php?0=
Server = /imageanation/next_prev_link_test.php

However when I click next it changes the url from

localhost/imageanation/next_prev_link_test.php

to

localhost/imageanation/next_prev_link_test.php?0=1

and reloads the same image. Hours of head scratching has not provided me with any solution, which I am sure will be quite simple if I could only see it. Edited by Zane
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.