-2
I’m trying to make a question and answer game in php, already working the comparison, however, I’m having a problem, when I click to answer the vector goes to the last Dice, ie, last question. I tried to put a Rand, but in this case he does the same thing and repeats the same question. I feel like I don’t know where to put the loop.
<?php
$banco = new mysqli("localhost", "root", "", "db_ingles");
$banco->set_charset('utf8');
if($banco->connect_errno != 0){
echo "<h1> Erro de conexao no banco</h1>Erro: ". $banco->connect_errno . "</h2>";
}else{
//conectado
}
$id = 0;
$pergunta= "";
$resposta= "";
$msg= "";
$erros = 0;
if (isset($_POST["btResponder"])) {
$id = $_POST["id"];
$pergunta = $_POST["pergunta"];
$resposta = $_POST["respostacorreta"];
if(strcasecmp($resposta, $_POST["resposta"]) == 0){
$msg = "<h1> acertou : $resposta </h1>";
$sql = "SELECT * FROM perguntas ORDER BY RAND() LIMIT 1";
$retorno = mysqli_query($banco, $sql);
$registro = mysqli_fetch_array($retorno);
$id = $registro["id"];
$pergunta = $registro["pergunta"];
$resposta = $registro["resposta"];
}else{
$msg = "<h1> Errada a resposta correta é: $resposta </h1> <br>";
}
}else{
// escolher uma pergunta
$sql = "SELECT * FROM perguntas ORDER BY RAND() LIMIT 1";
$retorno = mysqli_query($banco, $sql);
$registro = mysqli_fetch_array($retorno);
$id = $registro["id"];
$pergunta = $registro["pergunta"];
$resposta = $registro["resposta"];
}
?>
<?php
require_once '../config/header.inc.html'; ?> <!-- Importando o cabeçalho la do arquivo header -->
<div class="row container">
<div class="col s12">
<p> </p>
<!DOCTYPE html>
<html>
<head>
<title>perguntas</title>
</head>
<body>
<h3>Word: <?php echo $pergunta; ?></h3>
<form method="post" action="#" autocomplete="off">
<div class="input-field col s12 m6">
<input type="text" name="resposta" required autofocus/>
<label for="search">Digite a resposta</label>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="hidden" name="pergunta" value="<?php echo $pergunta; ?>">
<input type="hidden" name="respostacorreta" value="<?php echo $resposta; ?>">
<div class="input-field col s12 m6">
<input type="submit" name="btResponder" value="Responder" class="btn"/><br><br>
</div>
</form>
<?php echo $msg; ?>
<script type="text/javascript" src="materialize/js/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="materialize/js/materialize.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
});
</script>
</body>
</html>
</div>
</div>
<?php require_once '../config/footer.inc.html' ?> <!-- Importando o rodape la do arquivo footer -->
?>
This here
for ($i=0; $i<=10; $i++) { $registro = mysqli_fetch_array($retorno); }
will always return the last element in record, because it will be a loop of overwriting of the last added value. I don’t understand what you want to do, but if you want to get an array of all the answers you could dofor ($i=0; $i<=10; $i++) { $registro[] = mysqli_fetch_array($retorno); }
– Augusto Vasques
Another option is to use
fetch_all()
– Augusto Vasques
I simply want to pass position by position each time I click to answer. but maybe I’m not sure where to put the go in the right place
– maxmodle
So you have to put the operations that manipulate
$registro
within thefor
. What to do outside thefor
will always be in the last iterated record.– Augusto Vasques