Jump to content
Larry Ullman's Book Forums

gojira

Members
  • Posts

    2
  • Joined

  • Last visited

gojira's Achievements

Newbie

Newbie (1/14)

0

Reputation

  1. oh yeah, i get it now! thanks a lot, margaux! isset() in the following case returns TRUE even if the form field is blank, because the variable exists, so the print statement will never be executed: if (isset($wordsArray)) { $wordsArray = explode (' ', $_POST['words']); } else { print "<p>Please enter at least two words in the box.</p>"; $okay = FALSE; } this is probably the better way to do it (i don't know if it's the best): if (isset($wordsArray)) { if ( empty($wordsArray) ) { print "<p>Please enter at least two words in the box.</p>"; $okay = FALSE; } else { $wordsArray = explode (' ', $_POST['words']); } }
  2. this is what i came up with: pursue #3: "Create another script that creates and displays a multidimensional array (or some of it, anyway)." // create a multidimensional array: $pasta = array(1 => 'mozarella', 'tomato sauce', 'salmon', 'salt', 'pepper'); $rice = array(1=> 'black rice', 'white rice', 'some vegetables', 'olive oil'); $food = array( 'pasta' => $pasta, 'rice' => $rice, 'other' => 'salad', ); // display the multidimensional array: foreach ($food as $key => $value) { print "<p>For making $key we need the following ingredients:<br />\n"; foreach ($value as $key => $value) { print "<p>Ingredient $key is $value <br />\n"; } } // we get a small error here - i'm not sure why, but it's probably because foreach() can only be used with multidimensional arrays, and the last $key => $value pair in the $food array isn't another array, which would make that particular line non-multidimensional pursue #4: "Rewrite list.php so that it uses foreach instead of implode(), but still prints each sorted word on its own line in the rowser. Also add some form validation so that it only attempts to parse and sort the string if it has a value." i made it work with this code: $okay = true; $wordsArray = $_POST['words']; // turn the incoming string, $_POST['words'], into an array and validate: if (empty($wordsArray)) { // the validation print "<p>Please enter at least two words in the box.</p>"; $okay= FALSE; } else { $wordsArray = explode (' ', $_POST['words']); } // sort the words alphabetically: if ($okay) { sort ($wordsArray); } // use foreach instead of implode & print: if ($okay) { print "<p>An alphabetized version of your list is: "; foreach ($wordsArray as $key => $value) { print "<br />\n $value "; } } print "</p>"; i did have some problems with pursue #4, however, because the validation doesn't seem to work using isset() - it completely ignores the condition and goes straight to printing "An alphabetized version of your list is: ", even if no words are written in the box: if (isset($wordsArray)) { $wordsArray = explode (' ', $_POST['words']); } else { print "<p>Please enter at least two words in the box.</p>"; $okay = FALSE; } what am i missing?!
×
×
  • Create New...