asexynerd Posted May 29, 2012 Share Posted May 29, 2012 Can someone guide me step by step on how to do the pursue of chapter 7. I am having a hard time understanding it: A. Rewrite soups2.php so that it displays the number of elements in the array without using a separate variable. Hint: You’ll need to concatenatethe count() function call into the print statement. For this one do I did : //Count and print the current number of elements: print "<p>The soups array originally had " . count($soups) . " events </p>"; //Add three items to the array: $soups['Thursday'] = 'Chicken Noodle'; $soups['Friday'] = 'Tomato'; $soups['Saturday'] = 'Cream of Broccoli'; //Count and print the number of elements again: print "<p>After adding 3 more soups, the array now has " . count($soups) . " elements.</p>"; Is that right? B.Create another script that creates and displays a multidimensional array (or some of it, anyway). < Don't know what to do. C.Rewrite list.php so that it uses foreach instead of implode(), but still prints each sorted word on its own line in the browser. Also add some form validation so that it only attempts to parse and sort the string if it has a value. This is where i am stuck for C: // Turn the array back into a string: if (isset($_POST['word']) and is_array('word')){ foreach () } Some help and better understand would be greatly appreciated. Link to comment Share on other sites More sharing options...
Larry Posted May 30, 2012 Share Posted May 30, 2012 A. Yes, that's exactly correct. Kudos! B. Just come up with another example in which you have a complex data set. For example, take your favorite albums and use those, with the songs on them. C. You'll need to start by exploding $_POST['word'] to turn it into an array. Then you loop through that array using foreach. Within the foreach, you can print each word with a after it. Let me know if you have more questions. Link to comment Share on other sites More sharing options...
asexynerd Posted May 31, 2012 Author Share Posted May 31, 2012 Thank you for the reply larry and thank you for explaining to me how to tackle the questions. I didn't want to start chapter 8 until I solved the purse for 7 but now since I am on ch 8 i will do both at the same time and write you the feedback. Link to comment Share on other sites More sharing options...
gojira Posted June 1, 2012 Share Posted June 1, 2012 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?! Link to comment Share on other sites More sharing options...
margaux Posted June 1, 2012 Share Posted June 1, 2012 HartleySan posted a good link on isset, empty and is_null on another thread http://virendrachand...pty-vs-is_null/ Link to comment Share on other sites More sharing options...
gojira Posted June 1, 2012 Share Posted June 1, 2012 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']); } } Link to comment Share on other sites More sharing options...
asexynerd Posted June 10, 2012 Author Share Posted June 10, 2012 here is part b for pursu chapter 7 for MD array <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Chapter 7 pursu</title> </head> <body> <h2> Console Wars</h2> <?php /* pursu chapter 7 B * creating a multidimensional array */ //creating a MD array: $company = array( 1 => 'Microsoft','Nintendo', 'Sony'); $console = array( 1 => 'Xbox 360','Wii U','Playstation 3'); $firstparty = array( 1 => 'Halo', 'Mario', 'God Of War'); $best = array( 'Maker' => $company, 'System' => $console, 'mascot' => $firstparty ); print "<p>The last-gen console race begin with {$best['Maker'][1]} Xbox 360"; print "<p>The first next-gen console will be Nintento's {$best['System'][2]}"; print "<p>The most brutal first party title is Sony's {$best['mascot'][3]}"; foreach ($best as $name => $consolz){ foreach ($consolz as $game => $consoles){ print "<p>The gaming system by company, console and flagship title: $consoles\n</p>"; } } ?> </body> </html> And for C the issue I have been having with PHP is that I can read the code but up until now I struggle with constructing my own code. Can you explain that part to me and how to construct that problem? Link to comment Share on other sites More sharing options...
Larry Posted June 14, 2012 Share Posted June 14, 2012 I'm sorry, it's not clear to me what you mean by "C", and therefore how I would suggest you construct the problem. Could you provide part of the prompt so that it's crystal clear? Link to comment Share on other sites More sharing options...
asexynerd Posted June 18, 2012 Author Share Posted June 18, 2012 What i meant was the 3rd and 4th pursue chapter 7 and for chapter 8 "Rewrite the password conditionals found in register.php as a nested pair of conditionals. Hint: See Chapter 6 for examples." Link to comment Share on other sites More sharing options...
Larry Posted June 18, 2012 Share Posted June 18, 2012 Okay, sorry to be daft, but it's still not clear. So by "B" above, you mean pursue #3 for Ch 7 (Create another script...), which you've done. Did you have questions about that or no? By "C" above, you mean pursue #4 for Ch 7 (Rewrite list.php so that it...). Is that the one you want some advice on? And when you mention the pursue prompt from Ch 8, #3 (Rewrite the password...), what is your question there? Again, apologies for not being more helpful, but right now I can't even tell what, exactly, you're asking. Link to comment Share on other sites More sharing options...
Vegasvikk Posted November 23, 2012 Share Posted November 23, 2012 A general question: Are there answers posted somewhere for the "Review" part of "Review and Pursue" at the end of each chapter? Thanks Vegasvikk Link to comment Share on other sites More sharing options...
Larry Posted November 23, 2012 Share Posted November 23, 2012 There are not. I came up with the questions and prompts while writing the book, but it was never my intention to formally create and distribute answers. Link to comment Share on other sites More sharing options...
Vegasvikk Posted November 23, 2012 Share Posted November 23, 2012 Thanks Larry-- BTW I am loving this book so far. I am powering through it so I can get to the Advanced PHP5 book you wrote and the PHP6. I have Safari Online so I have ALL the books you have up there bookmarked. I hope this is a good deal for you and you are fairly compensated by Safari; otherwise I could not get access to your wonderful material. Cheers, Vegasvikk Link to comment Share on other sites More sharing options...
Larry Posted November 26, 2012 Share Posted November 26, 2012 Thanks for the interest in the books. As for Safari Online, I have no idea how well I'm being compensated for it, but that's the life of the writer! Glad you're able to use the resources. Note that the "PHP 6" book is actually the third edition of that book and there's a newer fourth edition: PHP and MYSQL for Dynamic Web Sites. You would also read the PHP & MySQL book after this one and before the PHP advanced. 1 Link to comment Share on other sites More sharing options...
Cross127 Posted March 1, 2019 Share Posted March 1, 2019 (edited) I do not understand how to modify event.php so that it prints the selected days as an unordered list. <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Add an Event</title> </head> <body> <?php // Script 7.9 - event.php /* This script handle the event form. */ // Address error management, if you want. // Print the text: print "<p>You want to add an event called <b>{$_POST['name']}</b> which takes place on: <br>"; // Print each weekday: if (isset($_POST['days']) AND is_array($_POST['days'])) { foreach ($_POST['days'] as $day) { print "$day<br>\n"; } } else { print 'Please select at least one weekday for this event!'; } // Complete the paragraph: print '</p>'; ?> </body> </html> Edited March 1, 2019 by Cross127 Link to comment Share on other sites More sharing options...
Larry Posted March 2, 2019 Share Posted March 2, 2019 Just open the unordered list before the foreach loop and close it after. Then, within the loop, print $day within <li> tags instead of using break. Link to comment Share on other sites More sharing options...
Recommended Posts