I'm having trouble with web development work, which asks for the following:
1 - Build a page that draws an integer from 1 to 10 and ask the user what the "imagined" number is. should indicate whether the attempt made by the user is greater or less than than the number drawn and count the number of attempts.
3 - When the user can set the number, the program should classify the user as:
- > 1 attempt: very lucky;
- > From 2 to 3 attempts: lucky;
- > 4 to 5 attempts: normal;
- > More than 5 attempts: Below average;
The code I made is this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Atividade 8 Slide 9</title>
</head>
<body>
<div>
<form method="get" action="atividade9.php">
<fieldset>
<legend>Atividade 9</legend>
<label for="txt" accesskey="1">Nº imaginado</label>
<input type="number" id="int" name="a" placeholder="Digite aqui o número"/>
<button type="submit">Enviar</button>
</fieldset>
</form>
<?php
session_start();
if($_SESSION[1] == null || $_SESSION[2] == null)
{
echo 'ativando session';
$_SESSION[1] = 1;
$_SESSION[2] = rand(1, 10);
}
$i = $_SESSION[1];
$b = $_SESSION[2];
if (isset($_GET['a'])) {
$a = $_GET['a'];
}
if ($a == $b) {
if ($i == 1) {
echo '<p>muito sortudo</p>';
}
else if ($i < 3) {
echo '<p>sortudo</p>';
}
else if ($i < 5) {
echo '<p>normal</p>';
}
else {
echo '<p>abaixo da médio</p>';
}
session_destroy();
}
else if ($a > $b){
echo '<p>Maior que o número</p>';
$i++;
echo '<p>I = '. $i .' B = '. $b .'</p>';
$_SESSION[1] = $i;
}
else if ($a < $b) {
echo '<p>Menor que o número</p>';
$i++;
echo '<p>I = '. $i .' B = '. $b .'</p>';
$_SESSION[1] = $i;
}
?>
</div>
</body>
</html>
The page can only use php and html, and even using session I could not make the variables keep the value at every attempt of the user.