douji2520 2014-08-24 17:11
浏览 50

PHP多项选择测验 - 如何在80个问题中显示20个随机问题

My website is now done and this is the last thing that needs to be sorted. I have added in 80 questions into the database. I have chose to focus on Fractions (adding, subtracting, multiplication and division).

The quiz shows all of the 80 and the results show on screen once the user has completed the quiz!

I need some advice, I want the quiz to generate just 20 ramdom questions from the database. How can this be done? I was talking to someone and he said I would have to have a counter (20) and an array to help with this - has anyone got any ideas?

This is the quiz code so far:

    <head>
    <link rel="stylesheet" type="text/css" href="/css/style.css" />
</head>
<?php
require 'core/init.php';
include('inc/pheader.php'); ?>

<?php

session_start();
if(isset($_GET['question'])){
    $question = preg_replace('/[^0-9]/', "", $_GET['question']);
    $next = $question + 1;
    $prev = $question - 1;
    if(!isset($_SESSION['qid_array']) && $question != 1){
        $msg = "Sorry! No cheating.";
        header("location: start.php?msg=$msg");
        exit();
    }
    if(isset($_SESSION['qid_array']) && in_array($question, $_SESSION['qid_array'])){
        $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
        unset($_SESSION['answer_array']);
        unset($_SESSION['qid_array']);
        session_destroy();
        header("location: start.php?msg=$msg");
        exit();
    }
    if(isset($_SESSION['lastQuestion']) && $_SESSION['lastQuestion'] != $prev){
        $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
        unset($_SESSION['answer_array']);
        unset($_SESSION['qid_array']);
        session_destroy();
        header("location: start.php?msg=$msg");
        exit();
    }
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Quiz Page</title>
<script type="text/javascript">
function countDown(secs,elem) {
    var element = document.getElementById(elem);
    element.innerHTML = "You have "+secs+" seconds remaining.";
    if(secs < 1) {
        var xhr = new XMLHttpRequest();
        var url = "userAnswers.php";
            var vars = "radio=0"+"&qid="+<?php echo $question; ?>;
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
            alert("You did not answer the question in the allotted time. It will be marked as incorrect.");
            clearTimeout(timer);
    }
}
xhr.send(vars);
        document.getElementById('counter_status').innerHTML = "";
        document.getElementById('btnSpan').innerHTML = '<h2>Times Up!</h2>';
        document.getElementById('btnSpan').innerHTML += '<a href="quiz.php?question=<?php echo $next; ?>">Click here now</a>';

    }
    secs--;
    var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000);
}
</script>
<script>
function getQuestion(){
    var hr = new XMLHttpRequest();
        hr.onreadystatechange = function(){
        if (hr.readyState==4 && hr.status==200){
            var response = hr.responseText.split("|");
            if(response[0] == "finished"){
                document.getElementById('status').innerHTML = response[1];
            }
            var nums = hr.responseText.split(",");
            document.getElementById('question').innerHTML = nums[0];
            document.getElementById('answers').innerHTML = nums[1];
            document.getElementById('answers').innerHTML += nums[2];
        }
    }
hr.open("GET", "questions.php?question=" + <?php echo $question; ?>, true);
  hr.send();
}
function x() {
        var rads = document.getElementsByName("rads");
        for ( var i = 0; i < rads.length; i++ ) {
        if ( rads[i].checked ){
        var val = rads[i].value;
        return val;
        }
    }
}
function post_answer(){
    var p = new XMLHttpRequest();
            var id = document.getElementById('qid').value;
            var url = "userAnswers.php";
            var vars = "qid="+id+"&radio="+x();
            p.open("POST", url, true);
            p.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            p.onreadystatechange = function() {
        if(p.readyState == 4 && p.status == 200) {
            document.getElementById("status").innerHTML = '';
            alert("Thanks, Your answer was submitted"+ p.responseText);
            var url = 'quiz.php?question=<?php echo $next; ?>';
            window.location = url;
    }
}
p.send(vars);
document.getElementById("status").innerHTML = "processing...";

}
</script>
<script>
window.oncontextmenu = function(){
    return false;
}
</script>
</head>

<body onLoad="getQuestion()">
<div id="status">
<div id="counter_status"></div>
<div id="question"></div>
<div id="answers"></div>
</div>
<script type="text/javascript">countDown(45,"counter_status");</script>
</body>
</html> 
  • 写回答

2条回答 默认 最新

  • dstbtam8732 2014-08-24 17:17
    关注

    Let me start by saying I have never done a line of PHP in my life, so this might not work.

    Try this:

     for ($i = 1; $i <= 20; $i++) {
         $var = rand(1,80);
         //code to get answer from SQL database.
     }
    

    Duplicates may occur with this code. I would use an array to stop this, but I have absolutely no idea how to create one. In sudo code:

    define array called "array"
    start for statement to count 20 times.
    start nested for statement
    check if this random number is the same as any other in the array
    if not, continue
    if so, variable in for statement = value - 1 (for above code, the variable is called i)
    get random number and add it to the array.
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题