John Jones
John Jones

Reputation: 15

How do I randomize content using php?

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.

Upvotes: 1

Views: 337

Answers (2)

hamedbahrami
hamedbahrami

Reputation: 38

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

Upvotes: 2

brokedid
brokedid

Reputation: 899

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>');
}

Upvotes: 1

Related Questions