3

次のボタンをクリックするたびに、配列で検索された最初の要素にスタックします。ここに私のサンプルコードがあります:

<?php

$letter = 'A';

if (isset($_POST["next"])) 
{
    if(isset($next))
    {
        unset($letter);
        $letter = $next;
    }

    $alphabet = array('A', 'B', 'C', 'D', 'E');

    $get = array_search($letter, $alphabet);

    $next = $alphabet[$get + 1];

    echo $next;
}

?>

<form name="alphabet" method="post"> 
<input type="submit"  name="next" value="next"/>
</form>

出力は次のとおりです。

B

私の望ましい出力は次のとおりです。

A-> B-> C-> D

次のボタンをクリックするたびに次のすべての要素に移動する方法 & 最後の要素が表示されている場合は、最初の要素へのループのように、配列の最初の要素に移動します。$_GET は使いたくありません。$_POST が必要です。これを手伝ってくれませんか?ありがとうございました。

4

2 に答える 2

1

これを試して。現在の文字を非表示の投稿変数として渡します。

<?php

$alphabet = array('A', 'B', 'C', 'D', 'E');
$next = 'A';   //for the first call of page.
if (isset($_POST["next"])) 
{

    $letter = $_POST['letter'];

    $get = array_search($letter, $alphabet);

    $next = $alphabet[($get + 1)%count($alphabet)];  //for loop over array

}

echo $next;
?>

<form name="alphabet" method="post"> 
<input type="hidden"  name="letter" value="<?php echo $next;?>"/>
<input type="submit"  name="next" value="next"/>
</form>
于 2013-10-17T21:09:15.113 に答える