Jump to content
Larry Ullman's Book Forums

OkComputer

Members
  • Content Count

    11
  • Joined

  • Last visited

Community Reputation

0 Neutral

About OkComputer

  • Rank
    Newbie
  1. Hugo, the value of the input field for the terms is already preset in the html markup, you're really validating that the check box was checked.
  2. Thanx for the assistance. I've modified the code accordingly, and here's what I got now: $flag = true; $words_Array = explode (' ', $_POST['words']); $count1 = count($words_Array); if ($count1 < 5) { echo "<p>Please enter at least 5 words.</p>"; $flag = false; } foreach ($words_Array as $word) { if (is_numeric($word)) { echo "<p>Please do not enter any numbers.</p>"; $flag = false; } elseif (empty($word)) { echo "<p>You have entered an empty space as a word. Please enter at least 5 words.</p>"; $flag = false; } } if ($flag) { sort($words_Array); echo "<p>An alphabetized version of your list is: "; foreach ($words_Array as $word) { echo "<br />\n $word "; } echo "</p>"; } The script works fine for the most part. I noticed that if enter the words in lower case, they come in last. I'm thinking that the ucfirst() function is right for the job, so I'll see how I can fit it in. Also I'm going to see if I can get it down to one IF statement, like Larry mentioned. I appreciate your help margaux.
  3. Hi. First off, this is in regards to exercise dealing with using a foreach loop instead of implode(). I was having trouble with it, and of course looked up what was posted here already, but I want to do more. By the I mean I wanted to validate that the form data was not empty, had no numbers, and had a minimum of five words that will of course be alphabetized(though I'd rather being doing some analysis with it for simple patterns like the avg number of characters by word, number of vowels, number of constants, but let me just be simple about it for now). Here's the code(just a note, to give credit where it's due this is code I've altered that was posted here originally): $flag = true; $words_Array = $_POST['words']; $count1 = count($words_Array); // turn the incoming string, $_POST['words'], into an array and validate: if (empty($words_Array) || (is_numeric($words_Array) ) || ($count1 < 5) ) { print "<p>Please enter at least five words and no numbers.</p>"; $flag= false; } else { $words_Array = explode (' ', $_POST['words']); } //sort the words alphabetically if ($flag) { sort($words_Array); } if ($flag) { print "<p>An alphabetized version of your list is: "; foreach ($words_Array as $key => $value) { print "<br />\n $value "; } } print "</p>"; ?> Now, I've run this code and whether I enter five words or four words and a number, it still runs the print statement inside the first if statement "Please enter at least five words and no numbers." So what I'm I missing here logic wise? Also I'm thinking this code could much cleaner without having to be redundant by using three separate if statements, would an if-elseif-else be a good choice/best practice ? Lastly, can validation be done without using a boolean "flag" variable ? Assistance is greatly appreciated.
  4. I think i got it. You use the foreach loop to label/organize the nested arrays stored in the multidimensional array, in this case, $music, and then you pull out the info you want accordingly with the loop + formatting. The understanding being that you want the index names of the first two nested arrays, that store the genre name and artist name, repectively, and the value of the last array that stores the album titles for each artist. Do I have it right ?
  5. Just woke up. This is the first thing I loooked at after brushing my teeth. Thanx a lot. I'm going to look over the code to understand how and why it works because I'm more of the why something works type of guy you know. But again, thanx a lot.
  6. Maybe, I'm not formatting it right, could that be the case or I'm I just totally off base here ?
  7. Just can't seem to get my head around the strucure for the foreach loop. Getting various results, no avail as nothing's lining up. This is how I have it now, and I'm just too tired to think straight at this point. $music = array ( 'Jazz' => $jazz, 'Classic Rock' => $classic_rock, 'Alternativ Rock' => $alternative_rock, 'Indie Rock' => $indie_rock, ); foreach ($music as $a => $ { echo "Genre $a:<br />"; foreach ($b as $key => $value){ echo "<pre>" . print_r($key) . " : " . print_r($value) . "</pre>"; } echo "<br>"; foreach($value as $c => $d) { echo print_r($c) . ":" . $d; } }
  8. Thanx, I just made myself a cup of coffee and started googling through stackoverflow posts, the php manual, etc.. and you're right! So changed my codes for the three genres arrays to: $jazz = array ( 'Miles Davis' => array ('Kind of Blue', 'Bitches Brew', 'Sketches of Spain', 'Birth of the Cool'), 'John Coltrane' => array ('A Love Supreme', 'Blue Train', 'Giant Steps', 'My Favorite Things'), 'Dave Brubeck' => array ('Time Out', 'Time Further Out', 'Times Changes', 'Indian Summer'), ); $classic_rock = array ( 'The Beatles' => array ('Abbey Road', 'Sgt. Pepper\'s Lonely Hearts Club', 'Revolver', 'Rubber Soul'), 'Pink Floyd' => array ('The Dark Side of The Moon', 'The Wall', 'Wish You Were Here', 'Meddle'), 'The Doors' => array ('Doors', 'Strange Days', 'L.A Woman', 'Morrison Hotel'), ); $alternative_rock = array ( 'Pearl Jam' => array ('Ten', 'Vs.', 'Yield', 'No Code'), 'Radiohead' => array ('Pablo Honey', 'Kid A', 'O.K Computer', 'In Rainbows'), 'Dave Matthews Band' => array ('Under the Table and Dreaming', 'Busted Stuff', 'Crash', 'Before These Crowded Streets'), ); $indie_rock = array ( 'The National' => array ('Boxer', 'High Violet', 'Sad Songs for Dirty Lovers', 'Cherry Tree'), 'Interpol' => array ('Turn On The Bright Lights', 'Antics', 'Interpol', 'Our Love To Admire'), 'The Radio Dept.' => array ('Pet Grief', 'Clinging To A Scheme', 'Lesser Matters', 'Passive Aggressive'), ); and yes, I will need to create a third loop, no doubt.
  9. Hello, everyone. I created a multidimensional array that consists of 4 music genres in which each genre has 3 artists(using strings as keys) who in turn have 4 albums each(string values), but when I iterate over the multidimensioal array with a foreach loop, only the first album(the 1st string value) is associated with the artist(the string key). My intention was to map multiple string values to each string key/index, and have an output as such. Artist: Miles Davis Albums: Kind of Blue, Bitches Brew, Sketches of Spain, Birth of the Cool. Here's the code: <?php $jazz = array ( 'Miles Davis' => 'Kind of Blue', 'Bitches Brew', 'Sketches of Spain', 'Birth of the Cool', 'John Coltrane' => 'A Love Supreme', 'Blue Train', 'Giant Steps', 'My Favorite Things', 'Dave Brubeck' => 'Time Out', 'Time Further Out', 'Times Changes', 'Indian Summer', ); $classic_rock = array ( 'The Beatles' => 'Abbey Road', 'Sgt. Pepper\'s Lonely Hearts Club', 'Revolver', 'Rubber Soul', 'Pink Floyd' => 'The Dark Side of The Moon', 'The Wall', 'Wish You Were Here', 'Meddle', 'The Doors' => 'Doors', 'Strange Days', 'L.A Woman', 'Morrison Hotel', ); $alternative_rock = array ( 'Pearl Jam' => 'Ten', 'Vs.', 'Yield', 'No Code', 'Radiohead' => 'Pablo Honey', 'Kid A', 'O.K Computer', 'In Rainbows', 'Dave Matthews Band' => 'Under the Table and Dreaming', 'Busted Stuff', 'Crash', 'Before These Crowded Streets', ); $indie_rock = array ( 'The National' => 'Boxer', 'High Violet', 'Sad Songs for Dirty Lovers', 'Cherry Tree', 'Interpol' => 'Turn On The Bright Lights', 'Antics', 'Interpol', 'Our Love To Admire', 'The Radio Dept.' => 'Pet Grief', 'Clinging To A Scheme', 'Lesser Matters', 'Passive Aggressive', ); $music = array ( 'Jazz' => $jazz, 'Classic Rock' => $classic_rock, 'Alternativ Rock' => $alternative_rock, 'Indie Rock' => $indie_rock, ); foreach ($music as $genre => $artists) { print "<p>Genre: $genre"; foreach ($artists as $artist => $albums) { print "<br />" . "Artist: " . $artist . "\t" . "Albums: " . "$albums"; } print '</p>'; } ?> Here's the output: Genre: Jazz Artist: Miles Davis Album: Kind of Blue Artist: 0 Album: Bitches Brew Artist: 1 Album: Sketches of Spain Artist: 2 Album: Birth of the Cool Artist: John Coltrane Album: A Love Supreme Artist: 3 Album: Blue Train Artist: 4 Album: Giant Steps Artist: 5 Album: My Favorite Things Artist: Dave Brubeck Album: Time Out Artist: 6 Album: Time Further Out Artist: 7 Album: Times Changes Artist: 8 Album: Indian Summer Genre: Classic Rock Artist: The Beatles Album: Abbey Road Artist: 0 Album: Sgt. Pepper's Lonely Hearts Club Artist: 1 Album: Revolver Artist: 2 Album: Rubber Soul Artist: Pink Floyd Album: The Dark Side of The Moon Artist: 3 Album: The Wall Artist: 4 Album: Wish You Were Here Artist: 5 Album: Meddle Artist: The Doors Album: Doors Artist: 6 Album: Strange Days Artist: 7 Album: L.A Woman Artist: 8 Album: Morrison Hotel Genre: Alternativ Rock Artist: Pearl Jam Album: Ten Artist: 0 Album: Vs. Artist: 1 Album: Yield Artist: 2 Album: No Code Artist: Radiohead Album: Pablo Honey Artist: 3 Album: Kid A Artist: 4 Album: O.K Computer Artist: 5 Album: In Rainbows Artist: Dave Matthews Band Album: Under the Table and Dreaming Artist: 6 Album: Busted Stuff Artist: 7 Album: Crash Artist: 8 Album: Before These Crowded Streets Genre: Indie Rock Artist: The National Album: Boxer Artist: 0 Album: High Violet Artist: 1 Album: Sad Songs for Dirty Lovers Artist: 2 Album: Cherry Tree Artist: Interpol Album: Turn On The Bright Lights Artist: 3 Album: Antics Artist: 4 Album: Interpol Artist: 5 Album: Our Love To Admire Artist: The Radio Dept. Album: Pet Grief Artist: 6 Album: Clinging To A Scheme Artist: 7 Album: Lesser Matters Artist: 8 Album: Passive Aggressive Any help is appreciated, but what I'd really like to understand is what I'm doing wrong/ or missing here. Thanx.
  10. Hi, I was having similar issues with this as well, till of course I realised my error and what Larry was referring to. Anyhow, here's my code(Btw, all the code beforehand is the same as in the book including html info and opening and closing php tags): Code: foreach ($books as $key => $value) { echo "<pre>", print "<p>" . print_r($key) . ":" . print_r($value) . "</p>\n"; "</pre>"; } ------------------------------------------------------------------------------------------- Same as above, just shortened: foreach ($books as $key => $value) { echo "<pre>", print "<p>" . print_r($key) . ":" . print_r($value) . "</p>\n"; "</pre>"; } Output: PHP VQSArray( [1] => Getting Started with PHP [2] => Variables [3] => HTML Forms and PHP [4] => Using Numbers) 1:1 1 PHP Advanced VQPArray( [1] => Advanced PHP Techniques [2] => Developing Web Applications [3] => Advanced Database Concepts [4] => Security Techniques) 1:1 1 PHP and MySQL VQPArray( [1] => Introduction to PHP [2] => Programming with PHP [3] => Creating Dynamic Web Sites [4] => Introduction to MySQL) 1:1 1 Also did it without the <pre> tags: foreach ($books as $key => $value) { echo "<p>" . print_r($key) . ":" . print_r($value) . "</p>\n"; } Output: PHP VQSArray ( [1] => Getting Started with PHP [2] => Variables [3] => HTML Forms and PHP [4] => Using Numbers ) 1:1 PHP Advanced VQPArray ( [1] => Advanced PHP Techniques [2] => Developing Web Applications [3] => Advanced Database Concepts [4] => Security Techniques ) 1:1 PHP and MySQL VQPArray ( [1] => Introduction to PHP [2] => Programming with PHP [3] => Creating Dynamic Web Sites [4] => Introduction to MySQL ) 1:1
×
×
  • Create New...