foucquet Posted March 7, 2014 Share Posted March 7, 2014 (edited) 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:-IMAGENext 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 tolocalhost/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 March 7, 2014 by Zane Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 7, 2014 Share Posted March 7, 2014 "?$id="; should be "?id="; Quote Link to comment Share on other sites More sharing options...
Solution foucquet Posted March 8, 2014 Author Solution Share Posted March 8, 2014 Hmm, didn't spot that one - definitely an autopilot typo... Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.