PHP在第一个循环中获取数组中的前四项然后在第二个循环中获得下一个四项?

时间:2017-05-02 20:05:19

标签: php html

//First file

<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<div>
<form action="questions.php" method="post">
    Quiz Name: <input type="text" name="quizname" /><br/><br/>
Number of M.C.Q questions: <input type="number" name="mcq" min="0" /><br/><br/>
Number of True/False questions: <input type="number" name="truefalse" min="0" /><br/><br/>
<input type="submit" name="submit" value="Start Generating Questions" />
</form>
</div>
</body>
</html>


//Second file
<?php

if(isset($_POST['submit'])){ 
    $quizName = $_POST['quizname'];
    $mcq = $_POST['mcq'];
    $truefalse = $_POST['truefalse'];



    echo '<form action="finalquestions.php" method="post">';
    for($i=1;$i<=$mcq;$i++){
        echo 'Question:'.$i.' <input type="text" name="question1[]" /> </br></br>';
        echo '<input type="text" name="radiooptions[]" /> </br>
          <input type="text" name="radiooptions[]" /> </br>
              <input type="text" name="radiooptions[]" /> </br>
              <input type="text" name="radiooptions[]" /> </br>';
        echo '</br></br>';
    }

    for($i=1;$i<=$truefalse;$i++){
        echo 'Question:'.$i.' <input type="text" name="question2[]" /> </br></br>';
        echo '<input type="radio[]" name="question2">True</br>
          <input type="radio[]" name="question1">False</br>';
        echo '</br></br>';
    }
}

?>

<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<input type="submit" name="submit" value="Generate Quiz" />
</form>
</body>
</html>


//Third file
<?php

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

    //array of input elements named "question1"
    $question1 = $_POST['question1'];

    //array of input elements named "radiooptions"
    $radiooptions = $_POST['radiooptions'];

    //get the length of those array
    $question1length = count($question1);
    $radiooptionslength = count($radiooptions);

    //loop through array to get first input with four radiooptions inputs
    for($x = 0; $x < $question1length; $x++) {
        echo $question1[$x];
        echo "<br>";
        echo "<br>";
        for($i = 0; $i < $radiooptionslength; $i++) {
            echo $radiooptions[$i];
            echo "<br>";
            echo "<br>";
        }
    }
在数组的第一个循环中的人[问题1]我想在[问题1]的数组中得到第一个输入,在第二个循环中我想获得数组[radiooptions]的前4个输入,当循环得到第二次时希望它给出数组[question1]的第二个输入,并在[radiooptions]数组循环中我希望它给我接下来的4个输入,例如接下来的4个输入将是5,6,7,8 ......但是它给了我不同的[问题]输入,但同样的前4项数组[radiooptions]

1 个答案:

答案 0 :(得分:0)

您需要在数组上使用简单的分页功能执行以下操作。

<?php 

    $array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
    for($i = 1; $i < 5; $i++){
        $utility = new Utility();
        $utility->getItems($i, $array);
        echo '<br/>';
    }

    class Utility{
        public function getItems($index, $array, $count = 4){
            $min = ($index - 1) * $count;
            $max = $index * $count;

            for($k = $min; $k < $max; $k++){
                echo $array[$k] . ' ';
            }
        }
    }

?>