Array ids and GET function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

Array ids and GET function

Post by db579 »

Hi, I have a function that displays the first image in a photo album in large and all the photos in it as thumbnails. I have a next and previous button that allows you to scroll through them (in large) and by clicking a specific thumbnail you can load that picture in large. My problem is that the pictures are stored in an array and the one to be shown large is selected by $keyid.

My problem is that selecting a thumbnail resets the $keyid so that clicking next doesn't load the next image but the second. i.e. if you press the next arrow then click the 4th thumbnail and then click the next arrow again it loads the 2nd picture rather than the 5th as it should.

How can I avoid this happening?

This is my code:

Code: Select all

generateThumbnails();

$act = 0;

$keyid = (isset($_GET['keyid']) ? $_GET['keyid']:'0');

$Picturecount = (count(glob("" . $dir . "*.jpg")))/2;

$thumb_selected = (isset($_GET['thumb']) ? $_GET['thumb']:'');

$picture_array = glob("$dir*");

if ($thumb_selected !==""){
$dirName  = substr($thumb_selected,0,strpos($thumb_selected,basename($thumb_selected)));
$thumbName = basename($thumb_selected);
$thumbFile = $dirName.$thumbName;
$selected = str_replace('_th.jpg','.jpg',$thumbFile);
foreach ($picture_array as $search){
$keyid = array_search($selected,$picture_array);
$large = $picture_array[$keyid];
}}

else{
if($keyid > (2*$Picturecount-1)){
$keyid = ($keyid - (2*$Picturecount));}
if($keyid < 0){
$keyid = (2*$Picturecount+$keyid);}
$large = $picture_array[$keyid];}

echo "

	<tr><td><a href='?album=$album&keyid=" . ($_GET['keyid']-2) . "'>Previous</a></td><td></td><td align='right'><a href='?album=$album&keyid=" . ($_GET['keyid']+2) . "'>Next</a></td></tr>

	<tr><td colspan='3' height='300' width='400' align='center'><img src='$large' alt=''></td></tr><tr><td height='50'></td></tr>";

Thanks very much!
Last edited by db579 on Mon Aug 09, 2010 6:14 am, edited 1 time in total.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Stop array variable id resetting

Post by JakeJ »

I didn't look at all of your code but here's a mistake:

if ($thumb_selected !==""){

Make that:
if ($thumb_selected !=""){

You don't need two equal signs there.
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

Re: Stop array variable id resetting

Post by db579 »

Thanks for that hadn't noticed. Anyone able to help with the $keyid bit? Thanks
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Array ids and GET function

Post by shawngoldw »

Could you post the generateThumbnails code? It's hard to say definitively without seeing that code, all you may need to do is change your next and previous buttons to use $keyid instead of $_GET["keyid"]. This will work if you are updating that same $keyid when a thumbnail is selected.

Also, you need to make sure that $_GET["keyid"] and $_GET["thumb_selected"] have valid input. You check that $keyid is within a range but who ever said that it was in fact a number? And maybe thumb_selected is not a folder you expect it to be, maybe it's one with some more sensitive data.
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

Re: Array ids and GET function

Post by db579 »

Thank you so much!! Changing (GET['keyid']+2) to ($keyid+2) worked perfectly! Very grateful!
Post Reply