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

This is the code I've been trying to make work for a while, and while everything else is OK, the problem occurs when I run it. It shows the table as I want it, but under the table it posts:

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

I know the problem is around the "!=0" value, but no matter what I change it into, it either flushes the whole effort or repeats the same message.

4 Answers 4

2
  1. You missed ; in the following statement.

    echo "</table>"
    
  2. You are iterating in steps of 5. $i + $rows and your $table can go at most $table[4].
  3. The error/notice is because of the same.
1

I've gotten the same error code, but I managed to find the issue. The error message you're getting has to do with arrays. It seems you're referencing to an undefined array[key]: for($i=0; $i <= $count; $i = $i + $rows). $i = $i + $rows is the culprit. Example: $i = 0 and $rows=5; so 0 + 5 = 5 so array[5] and this doesn't exist in the array. It only goes to 4 with the $table array. Hope this helps you along and/or someone else.

-1
<?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>
1
  • 2
    Answers are more helpful if you provide some explanation of what you are doing and why it works.
    – elixenide
    Dec 10, 2013 at 16:17
-1

I think who posted this problem has solved it already. Just a little bit change on this line for($i=0; $i <= $count; $i = $i + $rows) can solve the problem.

for($i=0; $i <= $count; $i = $i + $rows) // for($i=0; $i < $count; $i = $i + $rows) use this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.