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

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

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

Post 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!
Last edited by pickle on Wed Jul 21, 2010 10:16 am, edited 1 time in total.
Reason: [syntax] tags require double quotes, not single
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

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

Post by Gargoyle »

Code: Select all

$keyid = $keyid + 2; 
this always yields 2.
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

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

Post 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!
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

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

Post by Gargoyle »

Code: Select all

$_POST['counter']++;
this always yields 1. $ _POST vars are terminated after execution of the script.
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

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

Post 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.
db579
Forum Commoner
Posts: 37
Joined: Sun Jul 18, 2010 6:23 pm

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

Post 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!
Post Reply