duanmin0941 2014-09-13 22:52
浏览 34

会话数组被覆盖

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>
  • 写回答

2条回答 默认 最新

  • douxie1894 2014-09-14 01:52
    关注

    You should store everything and check against the stored values.

    // 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));
    

    Here is a class version (just for kicks)

    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");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大