1

I am writing a quiz that pulls questions from a database. Its arranging the questions randomly, but currently the correct answer is always the last answer. How would I rewrite the answers so the correct answer is randomly placed in the selection. I was thinking of a for loop inside of the while loop but I am not sure how I would write it

the code is as follows :

<?php
$sql = 'SELECT * FROM quizquestions ORDER BY rand() LIMIT 5';
$result = $db->query($sql); ?>


        <?php 
            while($row = $result->fetch_assoc())
            {?>
                <div class="questions">
                    <p><?php echo $row['Question'] ?></p>
                    <p><input type="radio" name="<?php echo $row['Ans1Name'] ?>" /><?php echo $row['Ans1'] ?></p>
                    <p><input type="radio" name="<?php echo $row['Ans2Name'] ?>" /><?php echo $row['Ans2'] ?></p>
                    <p><input type="radio" name="<?php echo $row['Ans3Name'] ?>" /><?php echo $row['Ans3'] ?></p>
                    <p><input type="radio" name="<?php echo $row['CorrectAnsName'] ?>" /><?php echo $row['CorrectAns'] ?></p>
                </div>
            <?php }?>

UPDATE

I have gotten much further and am at the part where the score is being checked

My question is how to check if all of the fields are filled out, since I am using this code

<form action="quiz_submitted.php" method="POST">
            <?php $keys = array('Ans1','Ans2','Ans3','CorrectAns');
                    while ($row = $result->fetch_assoc()) 
                    {
                        shuffle($keys);
                        printf('<div class="questions">');
                        printf('<p>%s</p>', $row['Question']);
                            foreach ($keys as $k) 
                            {
                                printf('<p><input type="radio" name="%s" value="%s" />%s</p>', $row['QuesName'], $row[$k.'Value'], $row[$k]);
                            }
                         printf('</div>');
                    }
            ?>
            <input type="submit" name="quiz" value="Get Score!" style="margin-left: 475px;" />
            </form>

how would I target the names in the $_POST array? I tried using

if(isset($_POST['quiz'])) {
    if(!isset($_POST[$row['QuesName']])) {
        die('Please answer all questions');
    }
}

but it kills the page everytime.

2
  • 1
    If you had the answers in a separate table with keys into the questions table, you could use order by rand(). Commented Oct 13, 2014 at 22:22
  • two database-queries are most assuredly slower than a simple array-shuffle with numeric indices. Several hundred percent slower, in fact - one might diminish that performance-loss with UNIX domain sockets or named pipes for your DBMS but it wont change the fact that you'll have yourself a slower website like that, even with INNER JOINs and whatnot -- but im guessing OP wasnt talking about big webapps here, so that might work very well Commented Oct 13, 2014 at 22:34

2 Answers 2

2

Not sure if I am getting your question right. Maybe you could put the answers into an array right before your .questions div and randomly sort it via array_rand and use current and next function to access your randomly sorted array dynamically.

reference http://php.net/manual/en/function.array-rand.php and http://php.net/manual/en/function.next.php

Sign up to request clarification or add additional context in comments.

1 Comment

it is best to use shuffle because I notice if you use array_rand using the same size of the original array it isn't 'random'
1

You should think about your Database model, you could add a Reference Table where you're storing all your answers and mark with a Flag the correct Answer.

Having the Database model like now, you're limited to x Answers per Question (as many as you've columns for it).

But with this existing DB Model I'd suggest to solve the Problem like this (shuffle mixes the array) :

$sql = 'SELECT * FROM quizquestions ORDER BY rand() LIMIT 5';
$result = $db->query($sql);
$keys = array(
    'Ans1',
    'Ans2',
    'Ans3',
    'CorrectAns'
);
while ($row = $result->fetch_assoc()) {
    shuffle($keys);
    printf('<div class="questions">');
    printf('<p>%s</p>', $row['Question']);
    foreach ($keys as $k) {
        printf('<p><input type="radio" name="%s" />%s</p>', $row[$k.'Name'], $row[$k]);
    }
    printf('</div>');
}

3 Comments

I like this method I will consider changing the way the database is constructed
Yes, it's the same as your output. You can also change the way you output. But for info: If we make the output within the PHP tags with a php function or out of the php tags, if the output is the same then it has no affect to the Style of the Page. The Browser always parses the output of the page, so for him it's all html, no php! (PHP-> ServerSide Programming Language, HTML-> The output generated, parsing by the browser)
Thank you, I appreciate the help! I have to keep studying on the associative arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.