Jump to content

Iterating through an array of images


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.
Link to comment
https://forums.phpfreaks.com/topic/286798-iterating-through-an-array-of-images/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.