-3
I’m having difficulties in web development work, which asks for the following:
1 - Build a page that draws an integer number from 1 to 10 and ask the user what is the "imagined".
2 - Your page shall indicate whether the attempt made by the user is greater or less that the number drawn and count the number of attempts.
3 - When the user gets the number right the program should classify the user as:
-> 1 try: very lucky;
-> From 2 to 3 attempts: lucky;
-> From 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 couldn’t get the variables to keep the value every time the user tries.
Hello Pedro! I see you are a young programmer. Good! It got a little confusing for us to understand what you want to do with the "a" field, or with Session['2'], etc. Try to use some clearer variables and explain what logic you are using. So we can say if it is a good logic and give suggestions on how to improve the code.
– Humba
Thanks for the tips, is that I have little/no experience with php. I have to admit that even I didn’t know what I wanted besides persistence of the data received on page for action in itself and that could only be used php.
– Pedro Abreu Maia