通过阵列查看以显示值时的未定义偏移


Undefined Offset when looking through an array to display values

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

<?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.红色etc

如果您不想使用Session变量,则必须设置用户在隐藏输入标记中输入的值。以下是获得特定结果所需的代码:

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

编辑:

问题是,您首先没有在表单中回显$count,只是将其递增。因此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>