PHP测验数组将随机显示

时间:2019-02-11 09:33:14

标签: php

我目前正在使用PHP quiz with on screen results上的代码grade.php:

<?php 

$Questions = array(
    1 => array(
        'Question' => '1. CSS stands for',
        'Answers' => array(
            'A' => 'Computer Styled Sections',
            'B' => 'Cascading Style Sheets',
            'C' => 'Crazy Solid Shapes'
        ),
        'CorrectAnswer' => 'B'
    ),
    2 => array(
        'Question' => '2. What is the Capital of the Philippines',
        'Answers' => array(
            'A' => 'Cebu City',
            'B' => 'Davao City',
            'C' => 'Manila City'
        ),
        'CorrectAnswer' => 'C'
    )
);

if (isset($_POST['answers'])){
    $Answers = $_POST['answers']; // Get submitted answers.

    // Now this is fun, automated question checking! ;)

    foreach ($Questions as $QuestionNo => $Value){
        // Echo the question
        echo $Value['Question'].'<br />';

        if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
             echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
             echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
        } else {
            echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
            echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; $counter++;

        }

        echo '<br /><hr>'; 
                                if ($counter=="") 
                                { 
                                $counter='0';
                                $results = "Your score: $counter/2"; 
                                }
                                else 
                                { 
                                $results = "Your score: $counter/2"; 
                                }
            }                           echo $results;
} else {  
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
    <?php foreach ($Questions as $QuestionNo => $Value){ ?>

        <h3><?php echo $Value['Question']; ?></h3>
        <?php 
            foreach ($Value['Answers'] as $Letter => $Answer){ 
            $Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
        ?>
        <div>
            <input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
            <label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
        </div>
        <?php } ?>

    <?php } ?>
    <input type="submit" value="Submit Quiz" />
    </form>
<?php 
}
?>

代码工作正常,但我想知道如何能够随机显示问题(要在2个问题中随机显示1个)?我已经尝试过array_rand();但我无法在这些代码中实现。感谢您能提供的任何帮助,谢谢!!

5 个答案:

答案 0 :(得分:0)

希望我已正确理解您的要求。请检查以下代码是否有帮助:

<?php 

$Questions = array(
    1 => array(
        'Question' => '1. CSS stands for',
        'Answers' => array(
            'A' => 'Computer Styled Sections',
            'B' => 'Cascading Style Sheets',
            'C' => 'Crazy Solid Shapes'
        ),
        'CorrectAnswer' => 'B'
    ),
    2 => array(
        'Question' => '2. What is the Capital of the Philippines',
        'Answers' => array(
            'A' => 'Cebu City',
            'B' => 'Davao City',
            'C' => 'Manila City'
        ),
        'CorrectAnswer' => 'C'
    )
);

if (isset($_POST['answers'])){
    $Answers = $_POST['answers']; // Get submitted answers.

    // Now this is fun, automated question checking! ;)

    foreach ($Questions as $QuestionNo => $Value){
        // Echo the question
        echo $Value['Question'].'<br />';

        if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
             echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
             echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
        } else {
            echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
            echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; $counter++;

        }

        echo '<br /><hr>'; 
                                if ($counter=="") 
                                { 
                                $counter='0';
                                $results = "Your score: $counter/2"; 
                                }
                                else 
                                { 
                                $results = "Your score: $counter/2"; 
                                }
            }                           echo $results;
} else {  
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
    <?php 
    $totalQuestionCount = count($Questions);
    $randQuestions = rand(0, ($totalQuestionCount - 1));
    shuffle($Questions);
    foreach ($Questions as $QuestionNo => $Value) {

        ?>

        <h3><?php echo $Value['Question']; ?></h3>
        <?php 
            foreach ($Value['Answers'] as $Letter => $Answer){ 
            $Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
        ?>
        <div>
            <input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
            <label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
        </div>
        <?php } 

        if ($QuestionNo == $randQuestions) {
            break;
        }
        ?>
    <?php } ?>
    <input type="submit" value="Submit Quiz" />
    </form>
<?php 
}
?>

您可以根据需要进一步更新它。

答案 1 :(得分:0)

您可以做类似的事情

$questionToDisplay = $Questions[array_rand($Questions)];

您说无法执行array_rand。在这种情况下,您可以获得一个随机整数作为索引:

$questionToDisplay = $Questions[mt_rand(1, count($Questions))];

在两个示例中,我都将一个随机问题提取到$ questionToDisplay变量中。

答案 2 :(得分:0)

您可以使用shuffle($Questions)将数组随机化。

例如:

$Questions = array(
    1 => array(
        'Question' => '1. CSS stands for',
        'Answers' => array(
            'A' => 'Computer Styled Sections',
            'B' => 'Cascading Style Sheets',
            'C' => 'Crazy Solid Shapes'
        ),
        'CorrectAnswer' => 'B'
    ),
    2 => array(
        'Question' => '2. What is the Capital of the Philippines',
        'Answers' => array(
            'A' => 'Cebu City',
            'B' => 'Davao City',
            'C' => 'Manila City'
        ),
        'CorrectAnswer' => 'C'
    ),
    3 => array(
        'Question' => '3. What is the answer to life the universe and everything',
        'Answers' => array(
            'A' => 'What???',
            'B' => '42',
            'C' => 'I should read The Hitchhiker\'s Guide to the Galaxy'
        ),
        'CorrectAnswer' => 'B'
    ),
    4 => array(
        'Question' => '4. What is your name',
        'Answers' => array(
            'A' => 'Sir Galahad of Camelot',
            'B' => 'Sir Robin of Camelot',
            'C' => 'Sir Launcelot of Camelot'
        ),
        'CorrectAnswer' => 'C'
    ),
    5 => array(
        'Question' => '5. What is your quest',
        'Answers' => array(
            'A' => 'To seek a shrubbery',
            'B' => 'To seek the Holy Grail',
            'C' => 'To seek a coconut'
        ),
        'CorrectAnswer' => 'B'
    ),
    6 => array(
        'Question' => '6. What is your favorite color',
        'Answers' => array(
            'A' => 'What do you mean? An African or European swallow?',
            'B' => 'Blue.',
            'C' => 'Blue. No yel-- Auuuuuuuugh!'
        ),
        'CorrectAnswer' => 'B'
    )
);

shuffle($Questions);

var_dump($Questions);

输出

array (size=6)
  0 => 
    array (size=3)
      'Question' => string '5. What is your quest' (length=21)
      'Answers' => 
        array (size=2)
          'A' => string 'To seek a coconut' (length=17)
          'B' => string 'To seek the Holy Grail' (length=22)
      'CorrectAnswer' => string 'B' (length=1)
  1 => 
    array (size=3)
      'Question' => string '6. What is your favorite color' (length=30)
      'Answers' => 
        array (size=3)
          'A' => string 'What do you mean? An African or European swallow?' (length=49)
          'B' => string 'Blue.' (length=5)
          'C' => string 'Blue. No yel-- Auuuuuuuugh!' (length=27)
      'CorrectAnswer' => string 'B' (length=1)
  2 => 
    array (size=3)
      'Question' => string '1. CSS stands for' (length=17)
      'Answers' => 
        array (size=3)
          'A' => string 'Computer Styled Sections' (length=24)
          'B' => string 'Cascading Style Sheets' (length=22)
          'C' => string 'Crazy Solid Shapes' (length=18)
      'CorrectAnswer' => string 'B' (length=1)
  3 => 
    array (size=3)
      'Question' => string '4. What is your name' (length=20)
      'Answers' => 
        array (size=3)
          'A' => string 'Sir Galahad of Camelot' (length=22)
          'B' => string 'Sir Robin of Camelot' (length=20)
          'C' => string 'Sir Launcelot of Camelot' (length=24)
      'CorrectAnswer' => string 'C' (length=1)
  4 => 
    array (size=3)
      'Question' => string '3. What is the answer to life the universe and everything' (length=57)
      'Answers' => 
        array (size=3)
          'A' => string 'What???' (length=7)
          'B' => string '42' (length=2)
          'C' => string 'I should read The Hitchhiker's Guide to the Galaxy' (length=50)
      'CorrectAnswer' => string 'B' (length=1)
  5 => 
    array (size=3)
      'Question' => string '2. What is the Capital of the Philippines' (length=41)
      'Answers' => 
        array (size=3)
          'A' => string 'Cebu City' (length=9)
          'B' => string 'Davao City' (length=10)
          'C' => string 'Manila City' (length=11)
      'CorrectAnswer' => string 'C' (length=1)

答案 3 :(得分:0)

在表单上调用jshell> Security.getProviders("AlgorithmParameters.EC")[0] .getService("AlgorithmParameters", "EC").getAttribute("SupportedCurves") $10 ==> "[secp112r1,1.3.132.0.6]|[secp112r2,1.3.132.0.7]|[secp128r1,1.3.132.0.28]|[secp128r2,1.3.132.0.29]|[secp160k1,1.3.132.0.9]|[secp160r1,1.3.132.0.8]|[secp160r2,1.3.132.0.30]|[secp192k1,1.3.132.0.31]|[secp192r1,NIST P-192,X9.62 prime192v1,1.2.840.10045.3.1.1]|[secp224k1,1.3.132.0.32]|[secp224r1,NIST P-224,1.3.132.0.33]|[secp256k1,1.3.132.0.10]|[secp256r1,NIST P-256,X9.62 prime256v1,1.2.840.10045.3.1.7]|[secp384r1,NIST P-384,1.3.132.0.34]|[secp521r1,NIST P-521,1.3.132.0.35]|[X9.62 prime192v2,1.2.840.10045.3.1.2]|[X9.62 prime192v3,1.2.840.10045.3.1.3]|[X9.62 prime239v1,1.2.840.10045.3.1.4]|[X9.62 prime239v2,1.2.840.10045.3.1.5]|[X9.62 prime239v3,1.2.840.10045 ... 840.10045.3.0.18]|[X9.62 c2tnb431r1,1.2.840.10045.3.0.20]|[brainpoolP160r1,1.3.36.3.3.2.8.1.1.1]|[brainpoolP192r1,1.3.36.3.3.2.8.1.1.3]|[brainpoolP224r1,1.3.36.3.3.2.8.1.1.5]|[brainpoolP256r1,1.3.36.3.3.2.8.1.1.7]|[brainpoolP320r1,1.3.36.3.3.2.8.1.1.9]|[brainpoolP384r1,1.3.36.3.3.2.8.1.1.11]|[brainpoolP512r1,1.3.36.3.3.2.8.1.1.13]" 之前,应先随机化array。

使用two constants of NamedParameterSpec,如果您有5个以上的数组,则会得到随机值的差异

foreach()

答案 4 :(得分:0)

您可以像这样设置

<!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Carousel</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
      <style>
      .carousel-inner img {
          width: 100%;
          height: 100%;
      }
    
      </style>
    </head>
    <body>
    
    <div class="container">
      
      <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <!-- Indicators -->
        <ol class="carousel-indicators">
          <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
          <li data-target="#myCarousel" data-slide-to="1"></li>
          <li data-target="#myCarousel" data-slide-to="2"></li>
          <li data-target="#myCarousel" data-slide-to="3"></li>
        </ol>
    
        <!-- Wrapper for slides -->
        <div class="carousel-inner">
          <div class="item active">
            <img src="begin.png" alt="" style="width:100%;">
          </div>
    
          <div class="item">
            <img src="first.png" alt="" style="width:100%;">
          </div>
        
          <div class="item">
            <img src="second.png" alt="" style="width:100%;">
          </div>
    
          <div class="item">
            <img src="third.png" alt="" style="width:100%;">
          </div>
        </div>
    
        <!-- Left and right controls -->
        <a class="left carousel-control" href="#myCarousel" data-slide="prev">
          <span class="glyphicon glyphicon-chevron-left"></span>
          <span class="sr-only">Previous</span>
        </a>
        <a class="right carousel-control" href="#myCarousel" data-slide="next">
          <span class="glyphicon glyphicon-chevron-right"></span>
          <span class="sr-only">Next</span>
        </a>
      </div>
    </div>
    
    </body>
    </html>