Morning people hope you're all well. I really need some advice from yourselfs. As you might know I'm in the middle of creating my own website. The site will let users register and to log in.

I want to create a quiz I have found some code from this site gotcode.org/blog.php?id=290. I want to create a multiple choice quiz where there are 20 random questions from a bank of 100 questions in the database where users can take the quiz and test their knowledge. The quiz has got to be multiple choice and have a selection of 3 wrong answers and 1 correct answer with radio buttons and a button where the users can press "next" to the next question.

At the end of the quiz the users will then be able to see how well they have done with a percentage out of 100 to see how well they have done. With the questions and answers they have selected. I then want the answers to be put into the database and to show on the users personal profile where they can track their progress. It will be a list of each quiz they have taken with the time and date. Along with the amount of questions they have taken, along with they can click on each quiz (dated and timed) and they can see how well they have done and track their progress.

I know how i want it to look its now just getting it started. The above link I did get the quiz sorted and I did get it to work but some of the code now is out of date.

I'm asking you guys if you can give me any suggestions as to where or how I can start this. This website it a re-creation what I did when I was at university for my dissertation/ final year project to help people like myself who suffer from dyscalculia and its a learning aid to help them. The site now is a portfolio to show my skills and knowledge to help me to get my first graduate PHP web developer job. I would really appreciate any help you guys could give me.

Thanks in advance

Rich

Member Avatar for diafol

First stop for me would be to get my head around the DB schema. Then thinking about the calls and data structures required for the page(s). Other considerations would involve Ajax or Not Ajax. The DB can be simple or it can be more complicated depending on how you see the future of the app developing. For example, will you be offering different tests or will it always be just one topic and choosing random 20 questions from a table. Will you allow users to see their score as they proceed or just at the end of the test. Will they be able to re-take the test? Will they be able to go back to a previous question and alter it? Many, many considerations. So, a simple 'do it like this' probably won't help you as each implementation will be different. And anyway, this is a help forum, not a gimme site.

Believe it or not this is a request that crops up very often here on DW - at least once a month, so searching for it here should provide a wealth of advice.

I have managed to find the code from the quiz that I created a few months ago (from the website that I mentioned in the first post)

below is the code

<?php
//Connect with mysql
$db = new mysqli("127.0.0.1", "root", "", "quiz");

//Perform the query to choose random questions
$query = $db->query("SELECT * FROM `table` ORDER BY RAND() LIMIT 20");

while ($row = $query->fetch_assoc()):
    $question = $row['question'];
    echo $question . "<br />";
endwhile;

//close result
$query->close();
//Close connection
$db->close();

session_start();
if (isset($_GET['question'])) {
    $question = preg_replace('/[^0-9]/', "", $_GET['question']);
    $next     = $question + 1;
    $prev     = $question - 1;
} // <--- I added this brace. You need to double-check it logically belongs here
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);
    }

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

When I try and run the page on my local host I'm getting the following error message:

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\quiz.php on line 8

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.