Newbie having problems w/code in O'Reilly book

This is a discussion on "Newbie having problems w/code in O'Reilly book" within the PHP Forum section. This forum, and the thread "Newbie having problems w/code in O'Reilly book are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old May 29th, 2005, 21:53
Junior Member
Join Date: May 2005
Location: North Carolina
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Newbie having problems w/code in O'Reilly book

Hello All -

I am a total newbie - web designer looking to take the next step and have been wanting to learn PHP for awhile. I recently picked up O'Reilly Learning PHP5 - have been going through the chapters and doing the exercises at the end.

In Chapter 4 there is an exercise to create an array for city populations and have it total at the end. I copied the code from the official O'Reilly site and it has errors in it. In the end it prints everything out correctly, however, for each state listed it prints out an undefined index error.

Here is the code:

<?php
// Separate the city and state name in the array so we can total by state
$population = array('New York' => array('state' => 'NY', 'pop' => 8008278),
'Los Angeles' => array('state' => 'CA', 'pop' => 3694820),
'Chicago' => array('state' => 'IL', 'pop' => 2896016),
'Houston' => array('state' => 'TX', 'pop' => 1953631),
'Philadelphia' => array('state' => 'PA', 'pop' => 1517550),
'Phoenix' => array('state' => 'AZ', 'pop' => 1321045),
'San Diego' => array('state' => 'CA', 'pop' => 1223400),
'Dallas' => array('state' => 'TX', 'pop' => 1188580),
'San Antonio' => array('state' => 'TX', 'pop' => 1144646),
'Detroit' => array('state' => 'MI', 'pop' => 951270));

// Use the $state_totals array to keep track of per-state totals
$state_totals = array();
$total_population = 0;

print "<table><tr><th>City</th><th>Population</th></tr>\n";
foreach ($population as $city => $info) {
// $info is an array with two elements: pop (city population)
// and state (state name)
$total_population += $info['pop'];
// increment the $info['state'] element in $state_totals by $info['pop']
// to keep track of the total population of state $info['state']
$state_totals[$info['state']] += $info['pop'];
print "<tr><td>$city, {$info['state']}</td><td>{$info['pop']}</td></tr>\n";

}

// Iterate through the $state_totals array to print the per-state totals
foreach ($state_totals as $state => $pop) {
print "<tr><td>$state</td><td>$pop</td>\n";
}
print "<tr><td>Total</td><td>$total_population</td></tr>\n";
print "</table>\n";
?>

I'm sure this is an easy fix - just not for a newbie

Thanks All!

  #2 (permalink)  
Old May 29th, 2005, 23:21
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
I've copied this code onto my server running PHP Version 4.3.10-10 and it works perfectly. You say you get an 'undefined index error'? Sounds like something to do with the $state_totals array. What is the exact error you get? Does is reference any particular lines? If you search for that specific error (ignoring your directory structure) on google, do you get any help on it?

It could be your version of PHP, it could also be a php configuration issue, however I don't see how(!).
  #3 (permalink)  
Old May 30th, 2005, 00:20
Junior Member
Join Date: May 2005
Location: North Carolina
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Hello Karl -

I'm running version 5.0.4 for PHP

The error I get is for each state on line 30 Undefined index. I did find a fix on another forum. If I put $state_totals[$info['state']] = ""; before line 28 so the first foreach reads:

foreach ($population as $city => $info) {
$state_totals[$info['state']] = "";
$total_population += $info['pop'];
$state_totals[$info['state']] += $info['pop'];
print "<tr><td>$city</td><td align=right>{$info['state']}</td><td align=right>{$info['pop']}</td></tr>\n";
}

then everything works fine

I think the problem was that the states were not being declared before being used in the $state_totals line.

Make sense??

Thanks for your reply - I'm sure you will be hearing a lot from me as I am determined to understand this - I can sit and read the books and understand what is being told, but when I go to actually apply it - - confusion sets it

Thanks!
  #4 (permalink)  
Old May 30th, 2005, 08:00
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
Aah, ok. I've figured this out.

PHP can be configured to tell you run time notices. You can change this behaviour in the php.ini file (although if you're using paid hosting you probably can't change it).

I previously had PHP setup to not post those messages as, by default, you simply want scripts to work and only post messages when something goes wrong. A user doesn't need to know about an unitialised array as long as the script works! Of course, in a development environment it's always handy to see these errors.

Quote:
; E_NOTICE - run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (e.g., using an uninitialized variable and
; relying on the fact it's automatically initialized to an
; empty string)
  #5 (permalink)  
Old May 30th, 2005, 14:56
Junior Member
Join Date: May 2005
Location: North Carolina
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Hey Karl -

Now that does make sense - confusing as it is for a newbie. I spent like three hours trying to figure out why I was getting the Undefined errors. For now I will keep it turned on for learning - I am running PHP locally on my computer and did the default installation as I have no idea what to leave on or off in the .ini file.

I'm off to tackle Chapters 5 and 6 - Working with Functions and Validating Forms - I think Chapter 7 is the good stuff - Working with Databases

I'm sure you'll be hearing a lot from me - thanks again!

Sqrlgrl
  #6 (permalink)  
Old May 31st, 2005, 10:19
Tim356's Avatar
Reputable Member
Join Date: Nov 2003
Location: Australia
Age: 25
Posts: 331
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tim356
Just thought I'd chime in here too to thank Karl. I tried running this script also to see if I got any errors and I have the same errors sqrlgrl has and I had no idea what was causing them.

So thanks Karl!
Closed Thread

Tags
newbie, having, problems, wcode, oreilly, book

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie with problems with Ap div aligment megb6806 Starting Out 4 Nov 17th, 2007 08:58
[SOLVED] Php code problems longstand PHP Forum 3 Oct 15th, 2007 10:53
Php code problems longstand PHP Forum 2 Oct 10th, 2007 20:47
[SOLVED] Newbie having a few CSS problems!! Graeme36 Web Page Design 18 Sep 29th, 2007 16:27


All times are GMT. The time now is 21:35.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43