正在覆盖会话数组


Session array being overwritten

我正在研究一个简单的"命运之轮"风格的算法。正确答案存储在$phrase中,用户可以提交一封信来猜测短语的一部分。

我的问题是,每次用户提交他们的猜测时,我的会话数组都会被吹走,并且不会像我希望的那样动态更新。

因此,理想情况下,如果答案是"boat",而用户的第一个猜测是"o",则显示以下内容:"#o##

第二个猜测是"t",下面显示:"#o#t"

如有任何建议或意见,我们将不胜感激。

<!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>

今天早上,我简化了代码,终于解决了这个问题。我不断地重写会话数组的所有内容,而不仅仅是匹配字符,这让我自己陷入了困境。我的下一步是将图像文件链接到每个字符,这样它就可以显示图形,而不仅仅是普通的html。

<?php
    // ***************************************** Game Initialization ***************************************

    // Set the phrase
    $phrase = "The Terminator";
    // Dump phrase into an array
    $split = str_split($phrase);

    // If Session array doesn't already exist, create it and dump each character of the phrase into it
    if (!isset($_SESSION['answer'])) {
        $_SESSION['answer'] = array();
        foreach ($split as $char) {
            array_push($_SESSION['answer'], $char);
        }
        $counter = 0; // Counter used to assign hidden characters to the phrase, change array contents " " if a space, "*" if a character
        foreach ($_SESSION['answer'] as $char) {
            if ($char == " ") {
                $_SESSION['answer'][$counter] = " ";
                $counter++;
            }
            else {
                $_SESSION['answer'][$counter] = "*";
                $counter++;
            }
        }
    }

    // ********************************************** Comparing Algorithm ***************************
    // Counter used in the comparrison algorithm
    $count = 0;
    // Check if user has submitted a guess
    if (isset($_POST['usr_guess'])) {
        // Dump user's guess into a variable
        $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;
                    echo $_SESSION['answer'][$count];
                    $count++;
            }
            else {
                echo $_SESSION['answer'][$count];
                $count++;
            }
        }
    }
?>

您应该存储所有内容并对照存储的值进行检查。

// Set the phrase
$phrase = "The Terminator";
// Dump phrase into an array
$split = str_split($phrase);
// Set container if not set
if(!isset($_SESSION['phrase'])) {
        // Save phrase
        $_SESSION['phrase']['full']     =   $phrase;
        // Split phrase for checking
        $_SESSION['phrase']['split']    =   $split;
    }
// If guess set, attempt process
if (isset($_POST['usr_guess'])) {
        // If not already in array, process
        if(!in_array($_POST['usr_guess'],$_SESSION['guess']))
            // Store each guess
            $_SESSION['guess'][]        =   strip_tags($_POST['usr_guess']);
    }
// Set a container array for letter to phrase storage
// For each letter check if there are spaces
foreach($_SESSION['phrase']['split'] as $_letter) {
        if(preg_match("/''s/",$_letter))
            $_comp[]        =   " ";
        // No spaces, check if instance of upper or lowercase letter in phrase
        else
            $_comp[]        =   (preg_grep("/".$_letter."/i",$_SESSION['guess']))? $_letter: '#';
    }       
// If no instances of # are in comp, juse echo saved phrase
if(!in_array("#",$_comp))
    echo $_SESSION['phrase']['full'];
// If # in comp, implode the current comp array
else
    echo ucwords(implode("",$_comp));

这是一个类版本(仅供参考)

class   VannaWhite
    {
        public  static function Play($phrase) {
                // Dump phrase into an array
                $split  = str_split($phrase);
                // Set container if not set
                if(!isset($_SESSION['phrase'])) {
                        // Save phrase
                        $_SESSION['phrase']['full']     =   $phrase;
                        // Split phrase for checking
                        $_SESSION['phrase']['split']    =   $split;
                    }
                // If guess set, attempt process
                if (isset($_POST['usr_guess'])) {
                        // If not already in array, process
                        if(!in_array($_POST['usr_guess'],$_SESSION['guess']))
                            // Store each guess
                            $_SESSION['guess'][]        =   strip_tags($_POST['usr_guess']);
                    }
                // Set a container array for letter to phrase storage
                // For each letter check if there are spaces
                foreach($_SESSION['phrase']['split'] as $_letter) {
                        if(preg_match("/''s/",$_letter))
                            $_comp[]        =   " ";
                        // No spaces, check if instance of upper or lowercase letter in phrase
                        else
                            $_comp[]        =   (preg_grep("/".$_letter."/i",$_SESSION['guess']))? $_letter: '#';
                    }       
                // If no instances of # are in comp, juse echo saved phrase
                if(!in_array("#",$_comp)) {
                        echo $_SESSION['phrase']['full'];
                        // Unset the session arrays ready for new word/phrase
                        self::Clear();
                    }
                // If # in comp, implode the current comp array
                else
                    echo ucwords(implode("",$_comp));
            }
        protected   static  function Clear()
            {
                unset($_SESSION['phrase'],$_SESSION['guess'],$_SESSION['answer']);
            }
    }
// Create Instance
VannaWhite::Play("The Terminator");