测验应用分数重置

时间:2016-06-24 16:10:13

标签: php

您好我的quizzer应用程序的处理文件。我的代码正在实现每个问题的答案,但每次我点击正确的答案并将分数设置为1。它转到下一个问题,然后将会话分数重置为0,然后添加一个。结果是,如果我得分为3,我实际上得分为1。我在哪里放置得分变量,以便它每次处理时都会重置?如果我echo $_SESSION['score'];最后,我总是得到0或1。

  $_SESSION['score'] = 0;
    if (isset($_POST['submit']))
    {
        $number = $_POST['number'];
        $selected_choice = $_POST['choice'];
        $next = $number + 1;

        // get total questions


        $query = "SELECT * FROM questions";


        // get result

        $results = mysqli_query($connection,$query);
        $total = mysqli_num_rows($results);

        // query to get right answer


        $query = "SELECT * FROM choices WHERE question_number = $number AND is_correct = 1";
        $result = mysqli_query($connection,$query);


        $row = mysqli_fetch_assoc($result);




        // set correct choice

        $correct_choice = $row['id'];
        // Compare

        if($correct_choice == $selected_choice)
        {
            // answer is correct
            $_SESSION['score']++;

        }


        // check is last question
        if($number == $total)
        {
            header("Location:final.php");
            exit();
        } else
        {
            header("Location:question.php?n=$next");
        }
    }

处理question.php

           <ul class = "choices">




                   <?php
                    while($row = mysqli_fetch_assoc($choices)): ?>
                   <li><input name = "choice" type = "radio" value = "<?php echo $row['id']; ?>" /> <?php  echo $row['text']; ?></li>
                    <?php  endwhile  ?>


                </ul>


            <input type = "submit" name = "submit" value = "submit">
            <input type = "hidden" name = "number" value = "<?php echo $number ?>" >

        </form>

2 个答案:

答案 0 :(得分:1)

您只是在说自己,在新页面加载时将分数重置为0。

$_SESSION['score'] = 0;

然后你要么对它做任何事情要么加1 - 所以那个得分怎么可能与0或1不同呢?停止重置分数。

答案 1 :(得分:1)

每次加载页面时,您都要将值设置为0:

$_SESSION['score'] = 0;

相反,在设置之前检查该值是否存在。也许就像这样简单:

if (!isset($_SESSION['score'])) {
    $_SESSION['score'] = 0;
}

每当页面加载时,只有在尚未设置的情况下才会设置该值。