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.
|
|
|
|
|
![]() |
||
Newbie having problems w/code in O'Reilly book
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
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! |
|
|
|
|||
|
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(!). |
|
|||
|
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! |
|
|||
|
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:
|
|
|||
|
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 |
![]() |
| Tags |
| newbie, having, problems, wcode, oreilly, book |
| Thread Tools | |
|
|
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 |