I am working on a simple "wheel of fortune" style algorithm. The correct answer is being stored in $phrase, and the user can submit a letter to guess a part of the phrase.
My problem is that each time the user submits their guess, my session array gets blown away and doesn't dynamically update like I would like.
So ideally if the answer is "boat", and the user's first guess is "o", the following displays: "#o##
Second guess is "t", the following displays: "#o#t"
Any suggestions or comments are greatly appreciated.
<!doctype html>
<?php session_start();?>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<!-- Form for user guess input -->
<form method="post" action="wof2.php">
Input Letter: <input type="text" name="usr_guess">
<input type="submit" value="submit">
</form><br/><br/><br/>
<?php
// Set the phrase
$phrase = "The Terminator";
// Dump phrase into an array
$split = str_split($phrase);
// Counter used in the comparrison
$count = 0;
if (!isset($_SESSION['answer'])) {
$_SESSION['answer'] = array();
foreach ($split as $char) {
array_push($_SESSION['answer'], $char);
}
}
if (isset($_POST['usr_guess'])) {
// User's guess
$usr_guess = $_POST['usr_guess'];
foreach ($split as $char) {
// Compares user guess to the answer and sets the answer in count position
if ($usr_guess == $char) {
$_SESSION['answer'][$count] = $usr_guess;
$count++;
}
else { // Checks for breaks in the word, used in accurately displaying spaces between words or # if it is a character
if ($split[$count] == " ") {
$_SESSION['answer'][$count] = " ";
$count++;
}
else {
$_SESSION['answer'][$count] = "#";
$count++;
}
}
}
}
?>
</body>
</html>