Jump to content

How to stop array id as variable resetting


db9

Recommended Posts

Hi, I think this is probably a logical error rather than a syntax one

 

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:

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>";

 

Any help would be massively appreciated. Thanks very much!

Link to comment
Share on other sites

I think what I want to do is have the GET['keyid'] bit check whether keyid has been set in my foreach loop or not yet before taking it from the url/setting it to zero. Is this possible/how would I do it? Thanks

Link to comment
Share on other sites

What is the use of foreach loop here. I find it redundant.

 

Try echoing the $keyid in the else part and just before calculating the $large.

 
else
{
echo "key value before |$keyid|";
  if($keyid > (2*$Picturecount-1)){
    $keyid = ($keyid - (2*$Picturecount));}
  if($keyid < 0){
    $keyid = (2*$Picturecount+$keyid);}
echo "key value after |$keyid|";
  $large = $picture_array[$keyid];
}

 

I think you can come to know then. Else paste the output.

Link to comment
Share on other sites

Hi thanks for the help. I tried echoing the $keyid like you said but all it does is output the right $keyid if the next arrow has been pressed but nothing if a thumbnail has been selected (as expected?) so I'm not really sure how that helps?

 

The foreach loop is supposed to go through each picture in the array and check if the $keyid matches the one of the thumbnail clicked. Why is that redundant? Thanks again.

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.