【问题标题】:Undefined offset: 5未定义的偏移量:5
【发布时间】:2013-01-25 03:30:31
【问题描述】:
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
    <?php
    $table = array ("1", "2", "3", "4", "5");
    $count = count($table); 
    echo "<table border='1'>";
    $rows = 5;
    for($i=0; $i <= $count; $i = $i + $rows)
    {
        echo "<tr>";
        for($z = 0; $z < $rows; $z++)
        {
            if ($table[$i + $z] !=0)
                echo "<td>This is row {$table[$i + $z]}</td></tr>";
            else
                echo "<td>&nbsp;</td></tr>";
        }
    }
    echo "</table>"
    ?>
</body>
</html>

这是我一段时间以来一直在尝试制作的代码,虽然其他一切都很好,但当我运行它时就会出现问题。它显示了我想要的表格,但它在表格下方发布:

Notice: Undefined offset: 5 in [file location] on line 22
Notice: Undefined offset: 6 in [file location] on line 22
Notice: Undefined offset: 7 in [file location] on line 22
Notice: Undefined offset: 8 in [file location] on line 22
Notice: Undefined offset: 9 in [file location] on line 22

我知道问题出在 "!=0" 值附近,但无论我将其更改为什么,它要么刷新整个工作,要么重复相同的消息。

【问题讨论】:

    标签: php html undefined offset


    【解决方案1】:
    1. 您在以下声明中错过了;

      echo "</table>"
      
    2. 您正在以 5 步进行迭代。$i + $rows 和您的 $table 最多可以转到 $table[4]
    3. 错误/通知是因为相同。

    【讨论】:

      【解决方案2】:

      我得到了相同的错误代码,但我设法找到了问题所在。您收到的错误消息与数组有关。看来您正在引用未定义的数组 [key]: for($i=0; $i &lt;= $count; $i = $i + $rows)$i = $i + $rows 是罪魁祸首。示例:$i = 0$rows=5; so 0 + 5 = 5 so array[5] 并且这在数组中不存在。使用 $table 数组时,它只能达到 4。 希望这对您和/或其他人有所帮助。

      【讨论】:

        【解决方案3】:
        <?php
        if(isset($_POST['submit'])){
            $the_file = file($_FILES['the_file']['tmp_name']);
            for( $i = 0; $i < count($myfile); ++$i){
                echo $myfile[$i] . "<br/>";
                echo "Line " . ( $i + 1 ) . " is  " .strlen($myfile[$i]) . " characters long<br/>";
            }
        }
        ?>
        
        
        <form method="post" action="myphp.php" enctype='multipart/form-data' >
            <input type="file" name="the_file" value="Yes"/>
            <input type="submit" name="submit" value="No"/>
        </form>
        

        【讨论】:

        • 如果您提供一些关于您在做什么以及为什么有效的解释,答案会更有帮助。
        【解决方案4】:

        我认为发布此问题的人已经解决了。只需在for($i=0; $i &lt;= $count; $i = $i + $rows)这一行稍作改动即可解决问题。

        for($i=0; $i &lt;= $count; $i = $i + $rows) // for($i=0; $i &lt; $count; $i = $i + $rows) 使用这个。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-20
          • 2018-07-27
          • 2013-08-18
          • 2011-03-13
          • 2022-01-02
          • 2011-07-22
          • 2014-10-01
          相关资源
          最近更新 更多