Page 1 of 1

Using (isset($_POST)) to return next value of array

Posted: Wed Jul 21, 2010 2:53 am
by db579
I have an array of images and I'm trying to create a next button so that when next is clicked the page displays the next image in the array. I'm calling the image by it's keyid and trying to increment this each time the next button is pressed. At the moment though it only works for the first time it's pressed after that nothing happens. This is the code I'm using:

Code: Select all


	$keyid = 0;


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

	
	if(isset($_POST['Next'])){

	$keyid = $keyid + 2;
	unset($_POST['Next']);
       }
      $large = $picture_array[$keyid];

    echo "

	<tr><td'><img src='$large' alt=''></td></tr>

	<tr><td><form method='post'><input type='image' name='Next' value='Next' img src='gallery/icons/arrow-blue-rounded-right.jpg' alt='Next'></form></td></tr>

Can anyone see where I'm going wrong? Thanks very much for any help!

Re: Using (isset($_POST)) to return next value of array

Posted: Wed Jul 21, 2010 3:53 am
by Gargoyle

Code: Select all

$keyid = $keyid + 2; 
this always yields 2.

Re: Using (isset($_POST)) to return next value of array

Posted: Wed Jul 21, 2010 4:39 am
by db579
Hi, thanks for the help. I've tried to change the code by introducing a counter variable so that it increments properly I still seem to have the same problem but I can't see why. Could you have another look at it?

Code: Select all


       	 $_POST['counter'] = isset($_POST['counter']) ? $_POST['counter'] : 0;

	if($_POST['Next']) {
	$_POST['counter']++;
	}

        echo "
      <form method='post'><input type='image' name='Next' Value='Next' img src='gallery/icons/arrow-blue-rounded-right.jpg' alt='Next'></form></td></tr>
Thanks very much!

Re: Using (isset($_POST)) to return next value of array

Posted: Wed Jul 21, 2010 5:29 am
by Gargoyle

Code: Select all

$_POST['counter']++;
this always yields 1. $ _POST vars are terminated after execution of the script.

Re: Using (isset($_POST)) to return next value of array

Posted: Wed Jul 21, 2010 8:29 am
by db579
Could you possibly suggest another way of doing it? I'm guessing my logic is flawed but I'm really stumped here. Thank you very much.

Re: Using (isset($_POST)) to return next value of array

Posted: Wed Jul 21, 2010 10:43 am
by db579
OK I found this way of doing it:

Code: Select all


<?php
session_start();
?>

<?php
if (empty($_SESSION['count'])){
    $_SESSION['count'] = 0;
}
else{
    $_SESSION['count']++;
}
echo $_SESSION['count'];

if (isset($_POST['submit'])){
    $value = empty( $_POST['value'] ) ? "" : $_POST['value'];
}

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="value" value="<? echo $value; ?>" />
<input type="image" img src="gallery/icons/arrow-blue-rounded-right.jpg" name="submit" id="submit" />
</form>
?>

The problem is that the page this would be on is called from another page using include() but the condition for the include is a variable which is determined by the URL.

Code: Select all

echo $_SERVER['PHP_SELF'];
bit seems to destroy the URL so that although this function works on its own, it causes nothing to be loaded in my context.

Thanks again for any help!