Undefined Offset

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Undefined Offset

Post by michaelk46 »

have this script (below) that works great, but it generates a notice when it runs.... Notice: Undefined offset: 3 in C:\wamp\www\test.php on line 14 (line 14 refers to line 13 in the below code)

Code: Select all

 
if ($_SERVER['REQUEST_METHOD'] != "POST")
    {
        include 'test.html.php';
    }
else
    {
        $a=0;
        $check = $_POST['check'];
        $number = count($check);
        while ($a <= $number)
            {
                print_r ($check[$a]);
                ++$a;
            }
    }
 
Here is the form code:

Code: Select all

 
<form action="" method="POST">
<input type="checkbox" name="check[]" value="1"  />
<input type="checkbox" name="check[]" value="2"  />
<input type="checkbox" name="check[]" value="3"  />
<input type="submit" name="export" value="Export to CSV"/>
</form>
 
How could I modify it so that it doesn't come up?
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Undefined Offset

Post by Reviresco »

The array has 3 items, so the index only goes up to 2.

Change:

Code: Select all

while ($a <= $number)
To:

Code: Select all

while ($a < $number)
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: Undefined Offset

Post by michaelk46 »

Thank You... That was easy
Post Reply