Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 8th, 2003, 03:59 PM
Registered User
 
Join Date: Oct 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chap 5 capitals.ph (array syntax)

First off, let me state that I'm a complete PHP newbie and don't have any prior programming experience. Although I'm very excited with the things I've learned thus far it has been frustrating mostly due to the register_globals off issue. I totally understand how the book would become outdated as newer and better versions of php are developed constantly. I respect the fact that the book iss teaching people the safer way of programming. However, it would be greatly helpful if on the errata site then that the updated code was published rather than have people struggle to figure it out. I've learned the way to call variables through this forum and a little guesswork which is cool but now am stuck again on arrays.

So anyway my issue is this. I've figured out how to call variables with register_globals off and have even found (with quite a bit of searching) the info on this forum to get arrays to work using the {} brackets. Somehow though I'm stuck on the states.php example. Can someone look at the code for the capitals.php below and advise as to what I'm doing wrong? I believe everything else is correct but nothing is displaying when I pick a state in states.php. Thanks in advance for your help.

Regards
Mark


<?php
$StateCapital = array (0 => "Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento","Denver","Hartford", "Dover","Tallahasse", "Atlanta", "Honolulu", "Boise", "Springfield","Indianapolis", "Des Moines", "Topeka", "Frankfort", "Baton Rouge","Augusta","Annapolis","Boston", "Lansing", "Saint Paul","Jackson", "Jefferson City", "Helena","Lincoln", "Carson City","Concord", "Trenton","Santa Fe", "Albany", "Raleigh","Bismarck","Columbus","Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia","Pierre", "Nashville", "Austin","Salt Lake City", "Montpelier","Richmond","Olympia","Charleston" , "Madison","Cheyenne");
for ($counter=0; $counter<50; $counter++)
{
    if($_POST["HiddenState"][$counter]==$_POST["State"])
    {
        echo "The State capital is {$StateCapital[$counter]}";
    }
}
?>

 
Old October 8th, 2003, 08:05 PM
Registered User
 
Join Date: Oct 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, sorry folks I should have checked the states page first before assuming it was the capitals page. There was a mistake there which was causing the problem.

Whoops.

It would be nice though for somebody to provide the proper code for the book if register_globals is off since this is the way that is so strongly suggested by everybody here and on the errata page.

Peace,
MTG

 
Old October 9th, 2003, 12:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem with that is that the authors all wrote the book a long time ago, and there's no reason for them to rewrite a LOT of code just because PHP has evolved. A new PHP4 book wouldn't last long on the shelves, since PHP5 is just around the corner.

Even if someone rewrote the code, there are still thousands of copies of the current book on the shelves, so anyone who picks up a copy is still stuck with the printed code.

Finally, there's the issue of consistency. A person that reads the book learns from the code because the text of the book should explain what the code does during its execution. If the code was all changed and patched up, these explanations might not follow the new code.

The simplest thing to do (and what Wrox did) is post a LARGE notice in the errata page saying that register_globals needs to be "on" for the code in the book to work.


Take care,

Nik
http://www.bigaction.org/
 
Old October 24th, 2003, 09:16 AM
dre dre is offline
Registered User
 
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to dre
Default

yar ... another newbie here !!!

I need some help please/ I am stuck at the same example in the book here, but the only difference is that i get all the information in the second array to display, and not just the value where the index id's are =.

Please see the two pages below. Any help/comments will be appreciated.

PS: pardon the sloppy code :D

Page 1:

<html>
<head>
    <title>Untitled</title>
</head>

<body>
<form method="post" action="words.php">
What word are you looking for?
<select name="word_one">

<?php

$letters = array('A','B','C','D','E');

for ($i = 0; $i < count($letters); $i++)
{ //open for:1
    echo '<option value='.$letters[$i].'>'.$letters[$i].'</option>'."\n";
} //close for:1

?>

</select>
<br>
<br>
<input type="Submit" value="Select">
<br>
<br>

<?php

for ($i = 0; $i < count($letters); $i++)
{ //open for:2
    echo '<input type="hidden" name="word_three[]" value='.$letters[$i].'><br>'."\n";
} //close for:2

?>

</body>
</html>


Page 2:

<?php

echo 'you have selected '.$_POST['word_one'].'<br><br>';

$words = array('Acid','Beta','Cheetah','Delta','Ember');

for ($i = 0; $i < count($words); $i++)
{ //open for:1

    if ($_POST['word_one'] == $_POST['word_three[$i]']);
    { //open if:1

        echo 'the word is: '.$words[$i].'<br>';
    } //close if:1

} //close for:1


?>

Thanks sooooo much
dre

 
Old October 24th, 2003, 10:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

For starters, you're not closing your <form> tag, and you're not quoting your attributes. This doesn't prevent your script from working, but it's incorrect HTML and should be fixed. Rich (quesadilla5) has some good tutorial posts about proper HTML style.

Your actual problems are due to these lines:

Quote:
quote:
    if ($_POST['word_one'] == $_POST['word_three[$i]']);
Okay. The first half of the condition is correct, but the right hand side is faulty. If you take a closer look, you'll realize that you're actually only accessing a single index into the $_POST array:

Code:
  $_POST['word_three[$i]']
         \______________/
                +----------> String index
Also, since your string is single-quoted, PHP performs no variable substitution within the string, so $i is literally the dollar sign followed by an i. It does not become 0, 1, 2, etc... as the variable $i's value increases.

So what you're really asking PHP to do is to compare the value at the index 'word_one' in the $_POST array with the value at the index 'word_three[$i]'.


What you WANT to do is this:
  if ($_POST['word_one'] == $_POST['word_three'][$i])

See the difference? Let's look at the right hand side more closely:

  $_POST['word_three'][$i]

We access array indexes from left-to-right. The more indexes we have, the farther nested we are into the array tree belonging to all the indexes above it.

$_POST['word_three'] is an array. This array is numerically indexed and created by your hidden input fields. Let's, for the sake of simplicity, assume that $my_array is the array represented by $_POST['word_three']. To access an index within $my_array, we use $my_array[index].

See, if $my_array is an alias for $_POST['word_three'], it follows that $my_array[$i] is equivalent to $_POST['word_three'][$i].



Take care,

Nik
http://www.bigaction.org/
 
Old October 24th, 2003, 11:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I should mention that your script can be totally rewritten to avoid all your hidden input fields. The problem with your hidden input fields is that it makes your script somewhat shaky and less efficient. The ONLY reason you're creating the word_three array is to have a map between some numerical index and a letter. You're using parallel arrays, when you don't need to.

To put it another way:

Code:
  index    word_three[index]      words[index]
    0           A                   Acid
    1           B                   Beta
    2           C                  Cheetah
    3           D                   Delta
    4           E                   Ember
Whenever the user submits something, you iterate through ALL of your words to see if the one the user submitted matches the word you're currently looking at that iteration. When you find the match, you continue to iterate over the rest of the words, even though know you won't have another match.

I know you're new to PHP, but I hope that you can see how much of a wasted effort this is.

Consider this rewrite of your application:

Code:
<html>
<head><title>Words</title></head>
<body>

<form method="post" action="words.php">

What word are you looking for? 

  <select name="word_one">
<?php
$letters = array('A','B','C','D','E');
$count   = count($letters);

for ($i = 0; $i < $count; $i++)
{
    echo "    <option value=\"{$letters[$i]}">{$letters[$i]}</option>\n";
}

?>
  </select>
  <br />
  <br />
  <input type="submit" value="Select" name="submit_button" />
  </form>
</body>
</html>
Notice that there are no hidden input fields and that all tags are properly nested and closed. Also, I assign the value of count($letters) to the variable $count for efficiency. In your original application, the function count($letters) was called for every iteration of the loop. This is totally unnecessary since the value is constant; you neither add nor remove items from the $letters array. Accessing the value of a variable is much quicker than calling a function.

Page 2:

Code:
<?php

$words = array('A' => 'Acid',
               'B' => 'Beta',
               'C' => 'Cheetah',
               'D' => 'Delta',
               'E' => 'Ember');

echo "You have selected {$_POST['word_one']}<br /><br />\n";

echo "The word is: {$words[$_POST['word_one']]} <br />\n";

?>
See the difference? I've created a string-indexed array, which is a direct map from the letter the user submits and the word associated with that letter. There are no iterations, you simply echo the word in the $words array who's index is the user input $_POST['word_one'].

Another difference: I use curly braces within double quotes to echo variable output to the screen. I like this method better than using single-quoted strings and concatenating variable values. I think it looks a bit cleaner.

Variables found within curly-braces in a string are parsed just as variables would be outside of the string, so you can access deeply nested array indexes without any problems. You still need to quote your array indexes as normal.

For more info on string types and variable substitution rules, read the manual:
  http://www.php.net/types.string



Take care,

Nik
http://www.bigaction.org/
 
Old October 31st, 2003, 12:40 PM
dre dre is offline
Registered User
 
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to dre
Default

Nik,

Thanks very much for the help. Really appreciated !!!!

dre:D






Similar Threads
Thread Thread Starter Forum Replies Last Post
dial a ph number automatically from asteriskjava abhinayak Java GUI 0 February 19th, 2008 02:40 AM
Access DB using VB to select msgs with capitals akb786 Access 36 May 23rd, 2005 07:44 AM
Capitals Hudson40 Access VBA 5 March 10th, 2005 01:10 PM
Page 73 - Array Syntax carlitio BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 August 18th, 2004 01:23 PM
session level array syntax requested lcsgeek Classic ASP Professional 4 January 27th, 2004 06:47 AM





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