Create form of radiobuttons

2

I'm doing a test platform. And on this platform, I have in the database a table of the questions it contains

  

Question, text, Question, AnswerCare

and the students_test table that contains

  

id Answer, Answer, id Question, idTest, IdUser

And I wonder how I can show different answers for each question. that is

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

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

If anyone could help, or take your time and talk to me via Skype, I was very grateful!

    
asked by anonymous 15.01.2017 / 22:08

1 answer

1

Good friend, your question is a bit incomplete as well as your database.

I could not fully understand if your problem is in relating and getting options for the questions or pulling them out of your database, however the structure you presented does not seem to fit what you need:

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

In order to fulfill what you need there are very easy ways, one of 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 'Answerback' field to store the correct answer. Or you could sort the answer questions, and create a table with the answers, and relate them by id or some other way.

However if your problem is with obtaining information from the database, 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;
    
16.01.2017 / 00:18