Show answers at random without repeating and post method

Asked

Viewed 62 times

0

Good, I have the following list of responses taken from my database. And in order for the right question not to be in the same place, I made an Random to show the results randomly. But the problem is that when listing, the results also repeat themselves, and I didn’t want such a thing to happen.

I would appreciate any help so that when doing the Random, the results would not be repeated.

And how can I do an answer post, 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;

2 answers

0

All value truly random, by definition, it will be repeated from time to time, especially if the amount of items involved is small.

To prevent the items from repeating in the same questionnaire, you would have to, as the questions were generated, store which items were used; if the random number repeated, you would have to draw it again.

In a more global scope, to reduce the chance that the values will be repeated, you could, for example, save statistics of how many times each question was used and make the most used questions less likely to appear.

Note: if you have a fixed size list, you can simply switch the pairs randomly, two by two.

  • I’ve thought about using shuffle, but I don’t know how to apply my code

0

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

To use this function you could do so:

$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 find out what is their correct answer by inspecting the radio.

  • this is pure php ??

  • Yes, why? Found something wrong with the code?

  • not just a little different from what I usually use, never used by ex " endforeach" and pq this to give error

  • i use endforeach when it has HTML inside it. Otherwise it’s easy to forget where the { below the code after. foreach with : instead of {. The same goes for the if and the for. Documentation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.