Jump to content

How would you make a button make the php select the next item in an array?


k21chrono

Recommended Posts

This a homework question?

<?php
$array = array(
'Item 1'
,'Item 2'
,'Item 3'
);

$curIndex = isset($_GET['index']) ? (int) $_GET['index'] : 0;
$nextIndex = $curIndex + 1 >= count($array) || $curIndex < 0 ? 0 : $curIndex + 1;

echo 'Current item: '. $array[$curIndex];
?>
<form method="post" action="?index=<?php echo $nextIndex; ?>">
<input type="submit" name="submit" value="Next Element" />
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.