简体   繁体   中英

PHP get first four item in array on first loop then get next four item on second loop?

//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>";
        }
    }

guys in first loop of the array [question1] I want to get first input in array of [question1] and in second loop I want to get the first 4 inputs of array [radiooptions] and when the loop gets second time I want it to give the second input of array [question1] and in the [radiooptions] array loop I want it to give me the next 4 inputs for example next 4 inputs will be 5,6,7,8.....But its giving me different [question] inputs but same first 4 item of array [radiooptions]

You would need to do something like the following using a simple pagination function on an array.

<?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] . ' ';
            }
        }
    }

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM