I have a questionnaire and I need to pass a list with the question and the value of the answer, I created a question table and an answer table in my database, but I am not able to pass the value of the answer in this list. >
1. database
tabela pergunta ->
idPergunta
pergunta
resposta
tabela resposta ->
idResposta
idUsuario fk idUsuario table usuario
idQuestionario fk idQuestionario table questionario
idpergunta fk idPergunta table pergunta
valor
2. The code in php is this:
<?php include ("cabecalho.php");
$result = getAllUsuarios();
while($row = mysqli_fetch_array($result)) {
?><h3><?php echo $row['nome'];?></h3>
<?php
$id = $row['idusuario'];
$resultRespostaUsuario = getRespostaByPerguntaByIdUsuario($id);
$questionario = getAllQuestionario();
while ($row2 = mysqli_fetch_array($questionario)) {
$idQuestionario = $row2['idquestionario'];
$resultPerguntaUsuario= getPerguntaByIdQuestionario($idQuestionario);
?><h4><?php echo $row2['nomeQuestionario']; ?> </h4>
<table class="table table-bordered table-striped">
<tr>
<td><a>pergunta</a></td>
<td><a>resposta</a></td>
</tr>
<?php
while($row3 = mysqli_fetch_array($resultPerguntaUsuario)){
?>
<tr>
<td><?php echo $row3['pergunta'];?></td>
<?php
while($row4 = mysqli_fetch_array($resultRespostaUsuario)) {
?>
<td><?php echo $row4['valor'];?></td>
</tr>
<?php }
}
?>
</table>
<?php }
} ?>
3. Functions:
function getAllUsuarios(){
$con = getConnection();
$sql = "select * from usuario";
return mysqli_query($con,$sql);}
function getRespostaByPerguntaByIdUsuario($id){
$con = getConnection();
$sql = "select * from resposta r
inner join questionarioPergunta qp
on qp.idpergunta = r.idpergunta where idusuario = $id";}
function getAllQuestionario(){
$con = getConnection();
$sql = "select q.*, user.nome nome from questionario q
INNER JOIN usuario user
ON q.idcriador=user.idusuario " ;
return mysqli_query($con,$sql);}
function getPerguntaByIdQuestionario($id){
$con = getConnection();
$sql = "select * from pergunta p inner join questionarioPergunta qp
on qp.idpergunta = p.idpergunta where idquestionario = $id";
return mysqli_query($con,$sql);}