Question system in php

Asked

Viewed 87 times

3

Hello guys I’m trying to think of a logica to make a question system with php but I’ve stopped in relation, when the user finishes asking question 1 it will give Submit to next then it would appear question number 2 and so on but my problem is that I am not able to think how to make it as soon as it der Ubmit it goes to question number 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 but I can not think how to leave automatic, so it ends question 1 ,will appear question two.

1 answer

2


A simple solution for what you want to do is to have a page that loads a question by a parameter of query string and in the validation of the question redirects to the same page 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 - checkPergunta.php

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

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

This solution will however browse page each time the user finalizes the question. If you want to get around this situation you can opt for a more complex solution and submit by AJAX and get the html of the next question as an answer to AJAX and dynamically update the page through Javascript.

Another simplistic but less good solution is to simply upload all relevant answers to the page and go showing and hiding through Javascript to show only the user will.

  • Cara started to shed some light on this, as you would show the data in the questions.php?

  • Query the database based on the id of the new question and place the values in the right places. Type Select * from questoes where id=$idPergunta. Then put $row["campoRelevante"]in the html paths that matter. To fetch the question the query string uses $idPergunta = $_GET["id"];

  • I understood the paquina will be redirected with the new id ,but how to capture this id when it is updating? example questions.php? id=1 ai it updates to questions.php? id=2 and how can I cap this id 2 on the questions page.php

  • Thus $idPergunta = $_GET["id"];. With the PHP get array you can access the "parameters" of the url, called query string. I will detail a little the answer to this scenario.

Browser other questions tagged

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