Create radio buttons form

Asked

Viewed 38 times

2

I’m doing a testing platform. And on this platform, I have in the database a table of questions that contains

idpergunta, textPergunta, Respostacerta

and the alunos_test table containing

idResposta, Reply, idPergunta, idTeste, Idaluno

And I would like to know how I can for each question demonstrate different answers. ie

Question 1 Bread and water ? A: 1 A: 2 A: 3

Question 2 Milk + olive oil ? A: 1 A: 2 A: 3

If anyone could help, or take their time and talk to me via Skype, I would be very grateful !

1 answer

1

Good friend, your question is somewhat incomplete, as well as your database.

I couldn’t quite understand if your problem is in relating and getting options to the questions or pulling them from your database, however the structure you presented doesn’t seem to suit what you need:

You have only 2 tables, one that contains not only the questions, but your answers, in string I imagine. And one to record the responses, or tests, performed by the users. However you wish to present multiple answers or options to each question, containing only one correct I imagine. However, it is not possible for you to create options randomly, not coherently, for your questions. Often multiple choice structures have their options previously created, which makes the options more coherent and even makes the question in question more complex.

To meet your needs there are easy ways, one of them you would need to just create 1 more field in your question table, where you would store 3 options for the question in question and modify the 'Answerright' field to store the correct answer. Or you could separate the questions from the answers, and create a table with the answers, and relate them by id or any other way.

However if your problem is in getting database information, this code example can help you:

if (!$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')):
    echo 'Não foi possível conectar ao mysql';
    exit;
endif;

if (!mysql_select_db('mysql_dbname', $link)):
    echo 'Não foi possível selecionar o banco de dados';
    exit;
endif;

$perguntaid = 1; //Neste exemplo, iremos obter respostas de uma única pergunta
$sql    = 'SELECT * FROM `perguntas` WHERE `idpergunta`="'.$perguntaid.'"';
$result = mysql_query($sql, $link);

if (!$result):
    echo "Erro do banco de dados, não foi possível consultar o banco de dados\n";
    echo 'Erro MySQL: ' . mysql_error();
    exit;
endif;

$row = mysql_fetch_assoc($result);
$respostacerta = $row['RespostaCerta'];
/*Caso siga minha sugestão de criar um campo com opções, e este campo seja uma string separando as opções por vírgula*/
$opcoes = $row['opcoes'];
$opcoes = explode(',', $opcoes);//separa as opções por vírgula
echo $row ['textoPergunta'];
foreach($opcoes as $opcao):
    echo $opcao;
endforeach;
  • If this is not the solution you are looking for, try to detail your question better and try to highlight where your problem really is

  • I can already list my questions, now the only problem is to list answers in different languages, so that if you answer the second question, not "select" what was selected in the first question

  • on different forms? Well, if an input is interfering with inputs from other questions, you’re probably using the same name for all of them, see this example with 2 https://jsfiddle.net/leonardorodrigues/bam38ocp/

  • That’s exactly what I added to the database, the wrong answers. now I wonder how you can list them in different forms for each question, ie assign dynamic names for each different answer

  • It was not very clear how you used to store the options of each question, whether it is within the table perguntas or within alunos_testes. If your answers are inside the table alunos_testes, separate from the questions, you can list them by a mysql call by selecting all the answers to a given idPergunta

  • In order for all questions to have options independent of the other questions, you need to assign different names to the inputs. As in the example I made, each question presents 3 inputs with their equal numbers for the options, but different among the questions

  • Yes, but I’m pulling the answers from the database, and I have no way to manually override

Show 2 more comments

Browser other questions tagged

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