Show answers randomly without repeating and post method

0

Well, I have the following list of answers taken from my database. And for the right question not to be in the same place, I did a random to show the results randomly. But the problem is that by listing, the results also repeat themselves, and I did not want such a thing to happen itself.

I would appreciate any help so that when doing the random, the results did not repeat themselves.

And how can I post a response, since I have them dynamically?

$radioname = "pergunta-" . $row['idPergunta'];
$items =[
    $row['RespostaCorrecta'],
    $row['RespostaErrada1'],
    $row['RespostaErrada2'],
    $row['RespostaErrada3'],
];

$rand_items = [
    $items[rand(0, count($items) - 1)],
    $items[rand(0, count($items) - 1)],
    $items[rand(0, count($items) - 1)],
    $items[rand(0, count($items) - 1)],
];

echo <<<HTML
    <fieldset name='pergunta-$radioname' id='pergunta-$radioname' >
        <input type='radio' required name='pergunta-$radioname' id='pergunta-$radioname' > {$rand_items[0]}
        <input type='radio' required name='pergunta-$radioname' id='pergunta-$radioname' > {$rand_items[0]}
        <input type='radio' required name='pergunta-$radioname' id='pergunta-$radioname' > {$rand_items[0]}
        <input type='radio' required name='pergunta-$radioname' id='pergunta-$radioname' > {$rand_items[0]}
    </fieldset>
HTML;
    
asked by anonymous 19.01.2017 / 16:45

2 answers

0

Every truly random value, by definition, will repeat itself from time to time, especially if the amount of items involved is small.

To avoid repeating items in the same quiz, you would have to, as questions asked, store which items were used; if the random number repeats, you would have to draw it again.

Already in a more global scope, to reduce the chance that values recur, you could, for example, keep statistics of how many times each question was used and make the most frequently asked questions less likely to appear.

Note: If you have a fixed size list, you can go by simply changing the pairs randomly, two at a time.

    
19.01.2017 / 16:54
0

PHP has the shuffle() function that changes an in-place , that is, it changes the original array.

To use this function you could do this:

$items = [
    $row['RespostaCorrecta'],
    $row['RespostaErrada1'],
    $row['RespostaErrada2'],
    $row['RespostaErrada3'],
];
shuffle($items);

Since you need to know the correct answer, I advise you to change the array structure:

$radioname = "pergunta-" . $row['idPergunta'];
$items = [
    [
        'resposta' => $row['RespostaCorrecta'],
        'correcta' => true,
    ],
    [
        'resposta' => $row['RespostaErrada1'],
        'correcta' => false,
    ],
    [
        'resposta' => $row['RespostaErrada2'],
        'correcta' => false,
    ],
    [
        'resposta' => $row['RespostaErrada3'],
        'correcta' => false,
    ],
];

shuffle($items);

echo "<fieldset name='pergunta-$radioname' id='pergunta-$radioname' >";

foreach ($items as $i => $item):
    $correcta = $item['correcta'] ? "resposta-correcta" : "" ;
    echo <<<HTML
    <label>
        <input type='radio' required name='$radioname' id='$radioname-$i' class='$correcta'> {$item['resposta']}
    </label>
HTML;
endforeach;

But I think it would be better to validate if the answer is right on the server, otherwise any user with a little knowledge can figure out what their correct answer is by inspecting radio .

    
19.01.2017 / 17:40