Php 查看数组以显示值时未定义的偏移量

Php 查看数组以显示值时未定义的偏移量,php,arrays,loops,echo,undefined,Php,Arrays,Loops,Echo,Undefined,嘿,我是PHP新手,我正在尝试从用户那里获取值,这些值被放入游戏的表单中。每次用户输入文本并单击submit时,都应将其添加到数组中。每次它都会在页面底部显示当前有序列表中的所有猜测,直到用户得到正确答案并获胜 <?php $count =0; $guesses = array(); if(isset($_POST['submit'])) { $count = $_POST['count'];

嘿,我是PHP新手,我正在尝试从用户那里获取值,这些值被放入游戏的表单中。每次用户输入文本并单击submit时,都应将其添加到数组中。每次它都会在页面底部显示当前有序列表中的所有猜测,直到用户得到正确答案并获胜

<?php

    $count =0;

    $guesses = array();

        if(isset($_POST['submit']))
        {   
            $count = $_POST['count'];
            for($r =0; $r < $count; $r++)
            {
                $guesses[$r] = $_POST['word'];
            } 
 ?>
    <h3>Guess the word I'm thinking</h3>
        <form action = "<?php echo $_SERVER['PHP_SELF'] ?>" method = "post">
            <input type = "text" name = "word" value = "<?php echo $tell; ?>"/>
            <input type = "hidden" name = "count" value = "<?php $count +=1;?>"/>
            <input type = "submit" name ="submit" value = "Make a Guess"/>          
        </form>

    <ol>
        <?php

            for($t=0; $t < $count; $t++)
            {
        ?>
        <li><?php echo $guesses[$t];?></li>

        <?php       
            }

        ?>
        </ol>
我一直得到一个未定义的偏移量:0。我做了一些阅读,我知道这与我填充数组错误或调用索引错误有关。希望你能帮助我解决这个问题。多谢各位

输出类似于:

你的猜测是: 1.蓝色 2.红色 等

编辑:

问题是,您并没有在表单中回显$count,只是简单地增加了它。因此post变量将为空。另外,在添加到数组后而不是在post中增加它

<?php
    if( !isset( $count ) ){
        $count =0;
    }
    $guesses = array();

    if(isset($_POST['submit'])) {   
            $guesses[$count] = $_POST['word'];
            $count++;
    }
?>
<h3>Guess the word I'm thinking</h3>
<form action = "<?php echo $_SERVER['PHP_SELF'] ?>" method = "post">
    <input type = "text" name = "word" value = "<?php echo $tell; ?>"/>
    <input type = "submit" name ="submit" value = "Make a Guess"/>          
</form>
<ol>
    <?php

        for($t=0; $t < count( $guesses); $t++)
        {
    ?>
        <li><?php echo $guesses[$t];?></li>
    <?php       
        }

    ?>
</ol>
请尝试以下代码:

<?php
$guesses = array();
if(isset($_POST['submit']))
{   
    if($_POST['guesses'] != '')
        $guesses =  explode('|', $_POST['guesses']);

    if(trim($_POST['word']))
        $guesses[] = trim($_POST['word']);
}

?>
<h3>Guess the word I'm thinking</h3>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    <input type="text" name= "word" value="" />
    <input type="hidden" name="guesses" value="<?php echo count($guesses)? implode('|',$guesses):''; ?>" />
    <input type="submit" name="submit" value="Make a Guess" />          
</form>
<ol>
<?php foreach($guesses as $guess) { ?>
            <li><?php echo $guess;?></li>
<?php   }  ?>
</ol>

如果您不想使用会话变量,那么您必须设置用户在隐藏输入标记中输入的值。以下是获得特定结果可能需要的代码:

<?php 
    $guesses="";
    if(isset($_POST['submit']))
    {   
        $guesses= $_POST['count']." ".$_POST['word'];
    }
?>
   <h3>Guess the word I'm thinking</h3>
   <form action = "<?php echo $_SERVER['PHP_SELF'] ?>" method = "post">
    <input type = "text" name = "word" value = "Word"/>
     <input type = "hidden" name = "count" value = "<?php echo $guesses;?>"/>
    <input type = "submit" name ="submit" value = "Make a Guess"/>          
   </form>

  <ol>
     <?php

        $count = explode(" ", $guesses);
        foreach($count as $val)
        {
            if($val!= ''){
    ?>

            <li><?php echo $val;?></li>

    <?php       
            }

        }

    ?>
    </ol>

您的问题是:,您将1添加到计数中,在最后一个for循环中,它试图访问未定义的偏移量。顺便说一句,为什么不使用会话?然后,您不能将计数发送回服务器。使用session\u start和$\u session[variable]来存储和检索数据。我们的游戏老师不希望我们使用sessions。我该如何解决这个问题?当一个新的猜测被提出时,它需要增加,我们把它带到我们自己,这样变量就不会重置没有错误,但它不会打印任何猜测,现在就尝试。另外,变量$tell似乎未定义?我忘了发送变量声明,我需要使用隐藏输入将计数增加1。此外,它还为$count$guessescloser设置了一个函数错误。计数从1开始,所以是1。医管局2。Hg3。。应该没有三个,除非有人点击提交,我也得到了一个错误。如果字段为黑色,用户单击“提交”按钮,则不应计算其非常接近的值。如果用户尝试不发送任何内容,则不应再次执行列表操作。谢谢。我可以很容易地把它调高一点。这到底是做什么的$猜测=$\u POST['count']$_贴子[单词];这个呢$计数=爆炸,$猜测;foreach$count as$val{if$val!={$guesses=$\u POST['count']..$\u POST['word'];用于扩展用户每次提交表单时输入的猜测。并且$count=explode,$guesses;foreach$count as$val{if$val!={…此代码用于将猜测字符串拆分为数组并在页面末尾显示。