Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 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 November 10th, 2003, 08:26 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default CH 5 pg 160

When the third script runs I get a unexpected T_Variable on line 3, which is $Count = 0; line. Why is this unexpected? It is set to 0 and is local.

<html><head></head>
<body>
    <form method = "POST" action = "dynamic.php">
    <p> How many children do you have? </p> <input name = "number" type = "text">
    <br />
    <input type = "submit">
    </form>
</body></html>

--------------
<html><head></head>
<body>
    <form method = "POST" action = "dynamic2.php">
    <?php
        for ($Counter = 0; $Counter< $_POST["number"]; $Counter++)
            {
                $Offset = $Counter + 1;
                echo "<br /> Please enter the name of child number $Offset<br />";
                echo "<input name = child[] type = text>";
            }
        if ($Counter == 0) echo "Press the button to move on.";
    ?>
    <br />
    <input type = "submit">
    </form>
</body></html>

------------------------
<html><head></head>
<body>
    <?pho $Count=0;
        echo "Your childrens names are: ";
    do
    {
        echo "<br />$_POST[ child[$Count]] ";
        $checkempty = "$_POST[child[$Count]]";
        $Count = $Count + 1;
    } while ($checkempty!="");
    if ($Count==1) echo "Not Applicable";
    ?>
</body>
</html>




Reply With Quote
  #2 (permalink)  
Old November 10th, 2003, 08:35 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok. scratch the last message. I realized in the third script the <?php was actually <?pho. But when I changed that I received this:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH5\dynamic2.php on line 7

Im sure that has to do with the array inside the Post but I dont know why.

Reply With Quote
  #3 (permalink)  
Old November 10th, 2003, 08:48 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I fixed the last error to. The $_POST[ child there was a space between { and child.

Fixed that and have still another error.

Parse error: parse error, unexpected '[', expecting ']' in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH5\dynamic2.php on line 7

I tried () and [] and I tried \[ \] but nothing worked. Im not sure why its saying this.


Reply With Quote
  #4 (permalink)  
Old November 10th, 2003, 09:11 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

Because this is invalid:

  $_POST[child[$count]]


What exactly are you trying to access? See, in your code, you have child[$count], which means you're treating the word 'child' like an array variable. It's not a variable, it's just a string literal.

If $child is an array, and the value of $child[$count] is the index you want to access in the $_POST array, your code would make sense.

The way I figure it, $_POST['child'] is an array, and you want to access the $count index of that array.

That's written like this:

$_POST['child'][$count]


Bear in mind that you cannot access nested array indexes within double-quoted strings using the regular variable substitution. Suppose $count is 3 in the following line of code:

  echo "it is $_POST[child][$count]."

This will print "it is Array[3]."

The reason is that $_POST['child'] is evaluated as an array. Since it's not a string (or can be converted to string), PHP substitutes the word "Array" for it's value. The next square bracket is echoed as a literal character, then $count is substituted with it's value, 3, and finally, the last square bracket character is printed.

You either need to use curly-brace syntax in your string, or close your string and concatenate your variable:

1) echo "It is {$_POST['child'][$count]}.";
2) echo "It is " . $_POST['child'][$count] . ".";

I prefer the first way. Notice that within curly-braces, you must quote all string indexes just as you would when accessing an array index from outside of a string.


Here are some links to the manual relevant to your problems:
  http://www.php.net/types.string
  http://www.php.net/types.array


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #5 (permalink)  
Old November 10th, 2003, 09:18 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

When you create an array of input elements using the empty brackets, you are creating a multidimensional array.

So... echo "<br />$_POST[ child[$Count]] ";
Isn't going to work.

The correct syntax is:
echo "<br />{$_POST["child"][$Count]} ";

Curly brace syntax is necessary here because PHP does not recognize more than a single layer of array nesting within strings. The curly braces essentially tells PHP to drop out of normal string parsing mode and treat the object between braces like a normal variable. Because you are no longer in normal string parsing mode, you won't be able to leave the quotes off of the indices, as doing so would cause PHP to look for a named constant first and the plain string indice second.

This will look for a constant named 'child' and throw an error notice under error_reporting = E_ALL
echo "<br />{$_POST[child][$Count]} ";

This will print the literal string child, as sadly, curly brace syntax does not do for constants what it does for variables, when there is only a constant between the braces.
echo "{child}";

Or you can just concatenate:
echo "<br />".$_POST["child"][$Count];

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #6 (permalink)  
Old November 11th, 2003, 10:13 AM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok. I got that. Thanks for the help.

I got it to work but I get some extra stuff:

 Your childrens names are:

wade .
natalie .
maria .
Notice: Undefined offset: 3 in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH5\dynamic2.php on line 7

.
Notice: Undefined offset: 3 in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH5\dynamic2.php on line 8


These two notices go on for hundreds of iterations.


Reply With Quote
  #7 (permalink)  
Old November 11th, 2003, 03:10 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

Your loop is a little screwy. It's more readable if you do it this way:

if (isset($_POST['child']) && !empty($_POST['child']))
{
    echo "Your children's names are: <br />\n";
    foreach($_POST['child'] as $child)
    {
        echo "{$child}.<br />\n";
    }
}

First of all, you're iterating the loop once without ever checking whether there ARE any children. That's bad.

Second, and most importantly, you echo the value of an array index, THEN create a string to hold that value, THEN check if that string is empty. That is completely backwards.

Also, the error messages suggest that you're not creating the $checkempty string properly, since it's ALWAYS != "".

If you're going to check whether a variable or an array index exists, you MUST use isset() to avoid getting undefined variable warnings when your error_reporting setting includes the E_NOTICE level.

It's best to write code with error_reporting set to E_ALL, the most string setting, because your code should then work on any other server setup, regardless of their error_reporting settings.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #8 (permalink)  
Old November 11th, 2003, 03:25 PM
Authorized User
 
Join Date: Oct 2003
Location: , Ok, .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nik,

just so your know, Im coping this straight out of the book and changing it the best I can with Globals off. The code is teaching a Do loop. But, I will use the If.

Wade

Reply With Quote
  #9 (permalink)  
Old November 12th, 2003, 11:23 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

That's a horrible way to teach a do-while loop. Sigh. I guess the author(s) can justify it saying that you wouldn't GET to the do-while loop unless you submitted the form that created children, but still!!!

Hope you're at least picking up how do-while, while, and for loops work. Let me know if you have any questions!

Take care,

Nik
http://www.bigaction.org/
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
asp.net 2.0 (VB) error try it out ch 5 pg 160 rdixon2005 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 January 10th, 2008 02:37 PM
Ch 5 Arrays Pg 134 Sadlowski BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 February 23rd, 2006 01:25 PM
Ch 2 pg 24 "Using the Properties..." LuluDev BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 2 January 23rd, 2006 09:12 AM
CH 4 Pg 143 HTMLSpecialChars wadesmart BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 November 3rd, 2003 02:29 PM
CH 3 pg 76 wadesmart BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 October 26th, 2003 08:46 PM



All times are GMT -4. The time now is 04:44 PM.


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