Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 3rd, 2004, 07:52 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loops and Arrays - Undefined offset notice

Hi all,

Can anyone tell me why i'm getting the following problem?

I've typed in the code for the dynamic.html, dynamic.php and dynamic2.php example in Beginning PHP4 - chapter 5.

I'm getting the following error when i go from dynamic.php to dynamic2.php...

Notice: Undefined offset: 1 in c:\inetpub\wwwroot\php\dynamic2.php on line 14

Notice: Undefined offset: 1 in c:\inetpub\wwwroot\php\dynamic2.php on line 15

Line 14 and 15 are:

echo"<BR><BR>$Child[$Count]";
$CheckEmpty = "$Child[$Count]";

It seems that the code gets to the final array item, moves to the next one and the check for empty actually results in an error as there is no array item to be found at all rather than it being set to blank...

I'm confused by this. Any good ideas out there?

Rob.

Reply With Quote
  #2 (permalink)  
Old January 3rd, 2004, 08:26 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

This is a horrible demostration of a do while loop.

In a nutshell you are getting these errors because of poorly written code. Notice level errors of undefined... something or another always means that a variable did not exist before you attempted to use it in a context like outputting it or comparing it. PHP automatically creates a copy of the variable in memory with null value temporarily if it doesn't exist... and then will complain in the form of a Notice level error.

For your reading pleasure:
http://www.php.net/isset
http://www.php.net/empty

These constructs will assist you in writing more explicit code and enable you to avoid the Notice level errors.

Here is a better demonstration of what the book is trying to accomplish:
Code:
<html>
    <head>
        <title>dynamic.html</title>
    </head>
    <body>
        <form method='post' action='dynamic.php'>
            How many children do you have? 
                 <input type='text' name='number' />
                <input type='submit' name='action' value='Continue' />
        </form>
    </body>
</html>

// dynamic.php

<html>
    <head>
        <title>dynamic.php</title>
    </head>
    <body>
        <form method='post' action='dynamic2.php'>
        <?php

            for ($i = 1; $i <= $_POST["number"]; $i++)
            {
                echo "Please enter the name of child {$i}:";
                echo "<input type='text' name='child[]' /><br />";
            }

            echo "<input type='submit' name='action' value='Continue' />";

        ?>
        </form>
    </body>
</html>
<html>
    <head>
        <title>dynamic2.php</title>
    </head>
    <body>
        Your children's name's are:<br />
        <?php

            for ($i = 0; $i < count($_POST["child"]); $i++)
            {
                if (!empty($_POST["child"][$i]))
                {
                    $_POST["child"][$i]."<br />";
                }

                else
                {
                    echo "You did not enter child #".($i+1)."'s name";
                }
            }

        ?>
    </body>
</html>
Hopefully that makes a little more sense? Please ask if you don't understand something! The book uses twice as much code as is really neccessary. And the do while loop that they use is actually the worst way to do that.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old January 3rd, 2004, 08:36 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Just for kicks.. this is an even better approach to 2 and 3:

// dynamic.php

if (isset($_POST["number"]) && is_numeric($_POST["number"]))
{
    for ($i = 1; $i <= $_POST["number"]; $i++)
    {
        echo "Please enter the name of child {$i}:";
        echo "<input type='text' name='child[]' /><br />";
    }
}

else
{
    echo "Error: you did not enter a valid number!";
}

http://www.php.net/is_numeric
The use of this function ensures that the data provided is a number. isset ensures that it exists, without isset if the variable did not exist you would get a notice level error.

// dynamic2.php

if (isset($_POST["child"]) && is_array($_POST["child"]))
{
    for ($i = 0; $i < count($_POST["child"]); $i++)
    {
        if (!empty($_POST["child"][$i]))
        {
            $_POST["child"][$i]."<br />";
        }

        else
        {
            echo "You did not enter child #".($i+1)."'s name";
        }
    }
}

else
{
    echo "You did not enter any of your children's names";
}

http://www.php.net/is_array
The use of is_array ensures that the variable is an array.

I am also using register_globals = off, which is why I use the $_POST superglobal array. Hundreds, if not thousands of posts on this topic in p2p alone.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
The Following User Says Thank You to richard.york For This Useful Post:
  #4 (permalink)  
Old January 4th, 2004, 06:40 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks and thanks and thanks again... totally clear now and quite infuriating that the examples in the books have now caused so many problems already for me that i've needed to decipher.

The thing about $_POST is clear and i discovered that one a while ago but thanks for the explanation of how i might like to use it.

I'm just trying to become clear enough that i can set myself a task to design some tool for a purpose and then learn as i go...

Wish me luck!

Rob.
Reply With Quote
  #5 (permalink)  
Old January 5th, 2004, 05:58 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The real reason this is a problem is because the default configuration settings of PHP versions earlier than 4.2.0 suppressed NOTICE level warnings, so many programmers thought that the above "bad" code was, in fact, "clever".

Sigh.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #6 (permalink)  
Old September 12th, 2005, 10:50 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Any help would be apprciated. I am have notices from the index I am using for the arrays. The code is below

<?php
         if ($teaches > 0){
            $eteachdata = explode(",", $teachdata);
            $estartdata = explode(",", $startdata);
            $eenddata = explode(",", $enddata);
            $mark = count($eteachdata);
            if ($mark > 0) {
                echo "<table width=\"600\" border=\"0\"><tr><td><p align=\"center\"><b>Teacher</b></p></td><td><p align=\"center\"><b>Start</b></p></td><td><p align=\"center\"><b>End</b></p></td><td><p align=\"center\"><b>Total</b></p></td></tr>";
                 for($i = 0; $i <= $mark; $i++){
    line 233 $id = $eteachdata[$i];
                    $sqldata = "SELECT * FROM `teachers` WHERE `id` = '$id'";
                    $result = mysql_query($sqldata);
                    $row = mysql_fetch_array($result);
                    $teacher = "{$row["firstName"]} {$row["lastName"]}";
    line 238 $total = $eenddata[$i]-$estartdata[$i];
    line 239 echo "<tr><td><p align=\"center\">{$teacher}</p></td><td><p align=\"center\">{$estartdata[$i]}</p></td><td><p align=\"center\">{$eenddata[$i]}</p></td><td><p align=\"center\">{$total}</p></td></tr>";
                }
            }
        }
        else{
            echo"<table width=\"300\" border=\"0\"><tr><td><p align=\"center\">No items books have been sorted yet.</p></td></tr></table>";
        }
        ?>

Here are the errors I am getting.

Notice: Undefined offset: 1 in c:\program files\easyphp\www\negaard\inventory.php on line 233

Notice: Undefined offset: 1 in c:\program files\easyphp\www\negaard\inventory.php on line 238

Notice: Undefined offset: 1 in c:\program files\easyphp\www\negaard\inventory.php on line 238

Notice: Undefined offset: 1 in c:\program files\easyphp\www\negaard\inventory.php on line 239

Notice: Undefined offset: 1 in c:\program files\easyphp\www\negaard\inventory.php on line 239


For now I can hide the errors but any good programmer should try and correct them right? Please help as soon as anyone can. Thanks in advanced.
Reply With Quote
  #7 (permalink)  
Old September 12th, 2005, 11:38 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually forget that I even posted that and I am sorry that I took up the digital space. I am a nut and realized that it was only doing that because I had "<=" it my for loop instead of "<". It works beautifully now.


Sorry
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined Offset 7thsense101 BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 December 9th, 2009 06:49 AM
Notice: Undefined index: username in latinquarter PHP Databases 1 April 18th, 2008 11:37 AM
undefined offset error trev Beginning PHP 9 January 24th, 2007 04:41 AM
Notice: Undefined offset: ## Herjan BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 November 27th, 2005 12:29 PM
ImageLine Undefined offset error ss2003 Beginning PHP 3 January 13th, 2004 04:37 PM



All times are GMT -4. The time now is 09:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.