0

I'm new to php and here's my code for a number guessing game. Since I don't know how to define a random number, I chose the number myself, 80. I'm trying to store all the guesses prior to the right one, and after the right guess, print them out on the screen. But I can't seem to be able to get it right since it only prints only the last guess before the right one.

Any help is appreciated!

<html>
<head>
</head>
<body>
<?php 
$allguesses = array();
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $t = $_POST["guess"];
    $sayi = 80;

    if($sayi >$t){
        echo 'Guess higher';
    }elseif($sayi == $t){
        echo "You've guessed it right!<br>";
        echo 'Guessed numbers: <br>';
        foreach($_POST["tmn"] as $y){
            echo $y . ',';
        }
    }else{
        echo 'Guess lower';
    }
    array_push($allguesses,$t);
}
?>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
<?php 
foreach($allguesses as $x){
    echo "<input type ='hidden' name = 'tmn[]' value=' ".$x . " '>";
}
?>
</form>

</body>
</html>
1
  • 3
    Use $_SESSION or some serverside storage - database,file, etc. Commented Mar 29, 2018 at 13:08

3 Answers 3

1

Sessions seem the best for where you are in your learning curve.

The session allows the normal stateless http protocol to remember things between each submission of a form or forms. So we will save each guess in an array of guesses in the SESSION, which in PHP is an array as well.

<?php 
session_start();  // create a session, or reconnect to an existing one

if( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["guess"]) ) {
    $_SESSION['guesses'][] = $_POST["guess"];   // keep an array of guesses

    // $t = $_POST["guess"]; no need for extra variables on the stack
    $sayi = 80;

    if($sayi > $_POST["guess"]){
        echo 'Guess higher';
    }elseif($sayi == $_POST["guess"]){
        echo "You've guessed it right!<br>";
        echo 'Guessed numbers: <br>';
        foreach($_SESSION['guesses'] as $guess){
            echo $guess . ',';
        }
        $_SESSION['guesses'] = array();  // clear the old guesses out
    }else{
        echo 'Guess lower';
    }
}
?>
<html>
<head>
</head>
<body>
<form method="post">
    Guess the number:
    <input type="number" name="guess" min ="1" max = "100"><br>
    <input type="submit" name="submit">
</form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Removed uneeded codes.

Changed method of showing previous guesses with the below code.

echo implode( ", ", $_POST["tmn"] ); // cleaner 

This block handles storing previous guesses into an array that is used for displaying previous guesses.

if( isset( $_POST ) ) {
        $_POST["tmn"][] = $t;
    }

Fixed previous version of below block of code so that hidden <inputs> of previous guesses are outputted properly..

<?php
    if( isset( $_POST["tmn"] ) ) {
        foreach($_POST["tmn"] as $x){
            echo "\n<input type ='hidden' name = 'tmn[]' value='$x'>";
        }
    }
?>

Updated Code:

<html>
<head>
</head>
<body>
<?php 
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $t = $_POST["guess"];
    $sayi = 80;

    if($sayi >$t){
        echo 'Guess higher';
    }elseif($sayi == $t){
        echo "You've guessed it right!<br>";
        echo 'Guessed numbers: <br>';
        echo implode( ", ", $_POST["tmn"] );
    }else{
        echo 'Guess lower';
    }
    if( isset( $_POST ) ) {
        $_POST["tmn"][] = $t;
    }
}
?>
<form method="post">
Guess the number:
<input type="number" name="guess" min ="1" max = "100"><br>
<input type="submit" name="submit">
<?php
    if( isset( $_POST["tmn"] ) ) {
        foreach($_POST["tmn"] as $x){
            echo "\n<input type ='hidden' name = 'tmn[]' value='$x'>";
        }
    }
?>
</form>

</body>
</html>

Comments

0

For random numbers you can use rand($min, $max). to store the guesses you can use the global variable $_SESSION.

<html>
    <head>
    </head>
    <body>
        <?php 
        //start session (needed to use the $_SESSION variable)
        start_session();
        if($_SERVER["REQUEST_METHOD"] == "POST"){

            //if empty -> initalize array
            if (empty ($_SESSION['allguesses']){
                $_SESSION['allguesses'] = array ()
            }

            $t = $_POST["guess"];
            $sayi = 80;

            if($sayi >$t){
                echo 'Guess higher';
            }elseif($sayi == $t){
                echo "You've guessed it right!<br>";
                echo 'Guessed numbers: <br>';

                //echo all guesses from $_SESSION variable
                foreach($_SESSION['allguesses'] as $y){
                    echo $y . ',';
                }

            }else{
                echo 'Guess lower';
            }

            //push in $_SESSION variable
            array_push($_SESSION['allguesses'],$t);
        }
        ?>
        <form method="post">
            Guess the number:
            <input type="number" name="guess" min ="1" max = "100"><br>
            <input type="submit" name="submit">
        </form>

    </body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.