Question system in php

3

Hello everyone, I'm trying to think of a logic to make a question system with php but I caught it in relation when the user finishes asking question 1 he will give submit to next there would appear question number 2 and so on but my problem is that I'm not able to think how to make it as soon as I submit it goes to issue 2 without being the same as before.

<?php

              while($row = $result->fetch_assoc())
                  {

                      $db_questoes = $row['id_questao'];

                      if($db_questoes == 1 ){

                         echo nl2br($row['questao_biologia']);

                       }

                  }

          ?>

Here is only the first one but I can not think how to make it automatic, as soon as question 1 is finished, it will appear question two.

    
asked by anonymous 06.07.2017 / 15:44

1 answer

2

A simple solution to what you want to do is to have a page that loads a query with a query string parameter and the query validation redirects to the same page by changing only the parameter:

>

File - questions.php? id = 1

<?php
      $idPergunta = $_GET["id"];
      $link = mysqli_connect("localhost", "utilizador", "password", "bd");

      if ($resultado = mysqli_query($link, "Select * from questoes where id = $id")) {
          $row = mysqli_fetch_assoc($resultado);
      }
?>
<html>
....

<form action="verificarPergunta.php">
     <div><?=$row["categoria"]?></div>
     <div><?=$row["pergunta"]?></div>
....

File - verifyPerhaps.php

....
$proximaPergunta = $perguntaCorrente+1; //ou escolhendo de forma aleatória
if ($perguntaCerta == true){
     header("Location:perguntas.php?id=$proximaPergunta");
}
...

Edit: I have edited the solution to complete the code with an example of fetching the question and displaying it in the html. For simplicity it does not include some validation tests such as checking that $_GET["id"] exists, or ensuring that it is correctly filled with mysqli_real_escape_string .

This solution will however navigate page every time the user finishes the question. If you want to work around this situation you can opt for a more complex solution and submit it by AJAX and get the html of the next question as an AJAX response and dynamically refresh the page through Javascript .

Another simplistic but less good solution is simply to load all relevant responses to the page and go showing and hiding through Javascript to show only what the user is going to.

    
06.07.2017 / 16:12