chrislancaster Posted March 27, 2011 Share Posted March 27, 2011 I love the book! I installed xampp and using netbeans 6.9.1 IDE ... the question I have is when i write script 2.6 and 2.7 the drop boxes are created but the arrays dont work the boxes have no options to choose from, they are blank.. does anyone know why? Thanks in advance -Chris Link to comment Share on other sites More sharing options...
Jonathon Posted March 27, 2011 Share Posted March 27, 2011 Hi Chris, If you post your code we'll be able to see if theres anything wrong with it. Otherwise i'm afraid it's pure guess work 1 Link to comment Share on other sites More sharing options...
chrislancaster Posted March 27, 2011 Author Share Posted March 27, 2011 I have checked and re-checked and it's the same as whats in the book. Here's what I wrote: <!-- To change this template, choose Tools | Templates and open the template in the editor. --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8\iso-8859-1"> <title>Multidimensional Arrays</title> </head> <body> <p> Some North American states, provinces and territories:</p> <?php // script 2.7 - multi.php // create one array $mexico = array('YU' => 'Yucatan', 'BC' => 'Baja California', 'Qa' => 'Oaxaca'); // create anther array $us = array('MD' => 'Maryland', 'IL' => 'Illinois', 'PA' => '*ennsylvania', 'IA' => 'Illinois'); // create a third array $canda = array('QC' => 'Quebec', 'AB' => 'Alberta', 'NT' => 'Northwest Territories', 'YT' => 'Yukon', 'PE' => 'Prince Edward Island'); // combine the arrays $n_america = array('Mexico' => '$mexico', 'United States' => '$us', 'Canada' => '$canada'); //Loop through countries foreach ($n_america as $country => $list) //print a heading echo "<h2>$country</h2><ul>"; //Print each state, province, or territory: foreach ($list as $k => $v) { echo "<li>$k - $v</li>\n"; } //close the list : echo'</ul>'; ?> </body> </html> I also get this error --Warning: Invalid argument supplied for foreach() in C:\Users\Owner\Desktop\xampp\htdocs\xampp\multi.php on line 47 if you can help I would greatly appreciate it ..none of the array scripts have worked for me Link to comment Share on other sites More sharing options...
Stuart Posted March 27, 2011 Share Posted March 27, 2011 Just remove the single quotes around the array names when you're combining the three arrays. The quotes make the elements a string which you can't pass to a foreach loop 1 Link to comment Share on other sites More sharing options...
Jonathon Posted March 27, 2011 Share Posted March 27, 2011 Your also missing the opening brace here: // Loop through the countries foreach ($n_america as $country => $list) { // missing brace and the subsequent closing one after the last echo 1 Link to comment Share on other sites More sharing options...
Jonathon Posted March 27, 2011 Share Posted March 27, 2011 This is my file for your reference and this works. also note the variable $canada in your script is miss-splet <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Untitled 1</title> </head> <body> <?php // Create one array $mexico = array( 'YU' => 'Yucatan', 'BC' => 'Baja California', 'OA' => 'Oaxaca' ); // Create another array $us = array( 'MD' => 'Maryland', 'IL' => 'Illinois', 'PA' => 'Pensilvania', 'IA' => 'Iowa' ); // Create another array $canada = array( 'QC' => 'Quebec', 'AB' => 'Alberta', 'NT' => 'Northwest Territories', 'YT' => 'Yukon', 'PE' => 'Prince Edward Island' ); // Combine arrays $n_america = array( 'Mexico' => $mexico, 'Canada' => $canada, 'United States' => $us ); // Loop through the countries foreach ($n_america as $country => $list) { // Print heading echo "<h2>$country</h2><ul>"; // Print each state or province foreach ($list as $key => $value) { echo "<li>$key - $value</li>\n"; } // Close List echo '</ul>'; } // End of main foreach ?> </body> </html> 1 Link to comment Share on other sites More sharing options...
chrislancaster Posted March 27, 2011 Author Share Posted March 27, 2011 OK got it ... thanks for everything ..I think I'll re-read the chapter to get a better understanding of { } Thanks again Link to comment Share on other sites More sharing options...
Recommended Posts