如何使数组中的元素在单击提交按钮后一次显示一个


How to make elements of an array appear one at the time after clicking submit button?

我有以下代码:

<?php
include_once 'init/init.funcs.php';
$pollid=(int) $_GET['pollid'];
$questions = array();
$result = mysql_query('SELECT kysimus FROM kysimused where kysimustik_id="' . $pollid . '"');
while($row = mysql_fetch_assoc($result)) {
    $questions[] = $row['kysimus'];
}
$count=count($questions);
$x=0;
while ($x<$count){
    echo $questions[$x];
    $x+=1;
}
?>
<form method="get">
<input type="submit" value="Submit">
</form> 

它从kysimustik_id=poll_id的数据库中提取问题,并对其进行echo处理。但我如何才能让它发挥作用,这样问题就会一次出现一个,只有在我点击"提交"按钮后才会出现新的问题?

你可以像下面的一样

<div class="row text-center">
        <button type="submit" class="btn btn-lg btn-info">Click Here!</button>
    </div>
$(function(){
        var values = [
            "hi",
            "hello",
            "nice",
            "wonderful"
        ];
        var counter = 0;
        $(".btn-lg").on("click", function(){
            if(values.length) {
                counter = (counter + 1) % values.length;
                $('.row.text-center').append(values[counter] + ' ');
            }
        });
});

在上面的例子中,我使用了javascript数组。您可以很容易地用php数组

替换它
<?php
include_once 'init/init.funcs.php';
$pollid=(int) $_GET['pollid'];
 if(isset($_POST['QuestionID'])
{
    $questionID = $_POST["QuestionID"];
    HandleAnswer($pollid,$questionID,$_POST['Answer']);
}
else
{
    $questionID = 0;
}
$questions = array();
$result = mysql_query('SELECT kysimus FROM kysimused where kysimustik_id="' . $pollid . '"');
while($row = mysql_fetch_assoc($result)) {
    $questions[] = $row['kysimus'];
}
function HandleAnswer(string PollId, string QuestionID, string Answer){
  // Implement piece of code that sets the answer in the database,
  // I leave this up to you since i have no Idea how your database is structured

}
 $questionID++;
 $count=count($questions);
 if($questionID < $count){
?>
<form method="post" action="mypage?=<?php echo $pollid; ?>">
<?php echo $questions[$questionID]; ?>
<input type="hidden" name="questionID" value="<?php echo $questionID;?>"/>
<input type="text" name="Answer"/>
<input type="submit" value="Submit">
</form> 
<?php }else{ ?>
THese were all our questions, thank you for answering!
<?php } ?>

我自己没有测试代码,但我确信它是有效的。请注意,这是一种非常不安全的方法,因为您可以使用firebug等扩展来编辑隐藏字段的值。