0

I'm trying to add 'guess' so that it shows in the text area without deleting the previous input. So that if you guessed '12' then '15' then '20'.

It would display in the text area as 12 15 20.

Rather than just displaying the most current value that you had entered for guess.

I've tried to put it into an array inside of the session, but the submit button is messing up the array when I try to display it inside of the textarea.

Any help would be greatly appreciated, thank you.

<?php
session_start();

if (!isset($_POST["guess"])) {
 $_SESSION["AmountofGuesses"] = 0; 
 $message = "Guessing Game";
 $_POST["Answer"] = rand(0,1000);
} 

else if ($_POST["guess"] > $_POST["Answer"]) { 
$message = $_POST["guess"]." is too high, try guessing lower.";
$_SESSION["AmountofGuesses"]++; 
} 

else if ($_POST["guess"] < $_POST["Answer"]) { 
$message = $_POST["guess"]." is too low, try guessing higher.";
$_SESSION["AmountofGuesses"]++; 
} 

else { 
$_SESSION["AmountofGuesses"]++;
$message = "You've guessed the correct number in, 
".$_SESSION["AmountofGuesses"]." guess/guesses! Click restart to start a new 
game."; 
unset($_SESSION["AmountofGuesses"]);
}

if (isset($_POST["guess"])) {

$button= $_POST["button"];

$ArrayofNumbers = array();
array_push($ArrayofNumbers,$_POST["guess"]);

if ($button=="Restart"){
$message = "Guessing Game";
$_POST["Answer"] = rand(0,1000);
$_SESSION["AmountofGuesses"] = 0;
}

if ($button=="Answer"){
$message = "You've given up, your answer is above. Click restart to start a 
new game.";
$_SESSION["AmountofGuesses"] = 0;
echo $_POST["Answer"];
    }
}
?>

<title>Guessing Game</title>
<h3><?php echo $message; echo $ArrayofNumbers;?></h3>
<form action "program1.php" method="POST">
<table border="2" cellspacing="6">
<td>
<br/>
<p>Guess: <input type="text" name="guess"/> </p>
<input type="hidden" name="Answer" value="<?php echo $_POST["Answer"]; ?>" >
<p>Number of guesses: <?php echo $_SESSION["AmountofGuesses"]; ?> </p>

<center><input type="submit" name="button" value="Submit"></center>
<br/>
<center><input type="submit" name="button" value="Restart"></center>
<br/>
<center><input type="submit" name="button" value="Answer"></center>

</td>

<td>
<textarea name="paragraph_text" cols="50" rows="10">
<?php if (isset($ArrayofNumbers)) {echo implode("\n", $ArrayofNumbers);}?>
</textarea>
</td>
</table>
</br>

<a href="index.html"> Back to Home</a>
Hatch
  • 11
  • 3
  • client side programming is definitely what you need – Rotimi Mar 13 '18 at 05:58
  • That's not really an option for me I need it server sided sadly. @Akintunde007 – Hatch Mar 13 '18 at 05:59
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Rotimi Mar 13 '18 at 05:59
  • _"I've tried to put it into an array inside of the session"_ - No, you append the answer to the array `$ArrayofNumbers`, but you never store it in your session. – M. Eriksson Mar 13 '18 at 06:07

2 Answers2

0

If you are ok with the output being similar to 12 15 20, then you can change $ArrayofNumbers to perhaps $PastGuesses and then append each next answer as a regular string.

$PastGuesses .= " " . $_POST["guess"];

And make sure $PastGuesses is still stored in $_SESSION.

Meghan
  • 1,215
  • 11
  • 17
0

You can add another New Session by using the command condition for your array as below:

  <?php session_start();
    if (!isset($_POST["guess"])) {
     $_SESSION["AmountofGuesses"] = 0; 
     $message = "Guessing Game";
     $_POST["Answer"] = rand(0,1000);
    }else if ($_POST["guess"] > $_POST["Answer"]) { 
    $message = $_POST["guess"]." is too high, try guessing lower.";
    $_SESSION["AmountofGuesses"]++; 
    }else if ($_POST["guess"] < $_POST["Answer"]) { 
    $message = $_POST["guess"]." is too low, try guessing higher.";
    $_SESSION["AmountofGuesses"]++; 
    }else { 
    $_SESSION["AmountofGuesses"]++;
    $message = "You've guessed the correct number in, 
    ".$_SESSION["AmountofGuesses"]." guess/guesses! Click restart to start a new 
    game."; 
    unset($_SESSION["AmountofGuesses"]);
    }

    if (isset($_POST["guess"])) {

    $button= $_POST["button"];
    if(@$_SESSION["guess"]!=''){
    array_push($_SESSION["guess"],$_POST["guess"]);
    }else{
    $ArrayofNumbers = array();
    array_push($ArrayofNumbers,$_POST["guess"]);
    $_SESSION["guess"]=$ArrayofNumbers;
    }

    if ($button=="Restart"){
    $message = "Guessing Game";
    $_POST["Answer"] = rand(0,1000);
    $_SESSION["AmountofGuesses"] = 0;
    unset($_SESSION["guess"]);
    }

    if ($button=="Answer"){
    $message = "You've given up, your answer is above. Click restart to start a 
    new game.";
    $_SESSION["AmountofGuesses"] = 0;
    unset($_SESSION["guess"]);
    echo $_POST["Answer"];
        }
    }
    ?>

    <title>Guessing Game</title>
    <h3><?=$message?></h3>
    <form action "" method="POST">
    <table border="2" cellspacing="6">
    <td>
    <br/>
    <p>Guess: <input type="text" name="guess"/> </p>
    <input type="hidden" name="Answer" value="<?=@$_POST["Answer"]?>" >
    <p>Number of guesses: <?=@$_SESSION["AmountofGuesses"]?> </p>

    <center><input type="submit" name="button" value="Submit"></center>
    <br/>
    <center><input type="submit" name="button" value="Restart"></center>
    <br/>
    <center><input type="submit" name="button" value="Answer"></center>

    </td>

    <td>
    <textarea name="paragraph_text" cols="50" rows="10">
    <?php if (@$_SESSION["guess"]!='') {
        foreach ($_SESSION["guess"] as $value) {
        echo $value."\n";
    }
    }?>
    </textarea>
    </td>
    </table>
    </form>