mstevens Posted October 25, 2014 Share Posted October 25, 2014 Hello! I am updating a current file which was using a simple array, I am updating it to use a multidimensional array. <!DOCTYPE html> <head> <title>The Chinese Zodiac</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <?php function validateInput($year, $fieldname) { global $errorCount; if (empty($year)) { echo ("'$fieldname' is a required field.</p>\n"); ++$errorCount; $retval = ""; } else { // if the field on the form has been filled in if(is_numeric($year)) { if($year >=1900 && $year <=2014) { $retval = $year; } else { ++$errorCount; echo "<p>You must enter a year between 1900 and 2014.</p>\n"; } } else { ++$errorCount; echo "<p>The year must be a number.</p>\n"; } } //ends the else for empty return($retval); } //ends the function function displayForm() { ?> <h1 style="text-align:center">The Chinese Zodiac</h1> <br></br> <table border="1" width=100%> <tr> <form action = "<?php echo $_SERVER['SCRIPT_NAME']; ?>" method = "post"> <th><p>Year of Birth: <input type="text" name="year" /></p></th> </tr> <tr> <th><p><input type="reset" value="Clear Form" /> <input type="submit" name="submit" value="Show Me My Sign" /></p></th> </tr> </table> </form> <?php } function StatisticsForYear($year) { global $year_count; $counter_file = "counts/$year.txt"; if (file_exists($counter_file)) { $year_count = file_get_contents($counter_file); file_put_contents($counter_file, ++$year_count); } else { $year_count = 1; file_put_contents($counter_file, $year_count); } return ($year_count); }?> </head> <body> <?php $showForm = true; $errorCount = 0; $zodiac=""; $start_year=1900; if (isset($_POST['submit'])) $year = $_POST['year']; validateInput($year, "Birth Year"); StatisticsForYear($year); if ($errorCount==0) $showForm = false; else $showForm = true; if ($showForm == true) { //call the displayForm() function displayForm(); } else { //begins the else statement //determine the zodiac $zodiacArray = array( "Rat" => array( "Start Date" => 1900, "End Date" => 2020, "President" => "George Washington"), "Ox" => array( "Start Date" => 1901, "End Date" => 2021, "President" => "Barack Obama"), "Tiger" => array( "Start Date" => 1902, "End Date" => 2022, "President" => "Dwight Eisenhower"), "Rabbit" => array( "Start Date" => 1903, "End Date" => 2023, "President" => "John Adams"), "Dragon" => array( "Start Date" => 1904, "End Date" => 2024, "President" => "Abraham Lincoln"), "Snake" => array( "Start Date" => 1905, "End Date" => 2025, "President" => "John Kennedy"), "Horse" => array( "Start Date" => 1906, "End Date" => 2026, "President" => "Theodore Roosevelt"), "Goat" => array( "Start Date" => 1907, "End Date" => 2027, "President" => "James Madison"), "Monkey" => array( "Start Date" => 1908, "End Date" => 2028, "President" => "Harry Truman"), "Rooster" => array( "Start Date" => 1909, "End Date" => 2029, "President" => "Grover Cleveland"), "Dog"=> array( "Start Date" => 1910, "End Date" => 2030, "President" => "George Walker Bush"), "Pig"=> array( "Start Date" => 1911, "End Date" => 2031, "President" => "Ronald Reagan") ); switch (($_POST['year'] - $start_year) % 6) { case 0: $zodiac = $zodiacArray[0]; break; case 1: $zodiac = $zodiacArray[1]; break; case 2: $zodiac = $zodiacArray[2]; break; case 3: $zodiac = $zodiacArray[3]; break; case 4: $zodiac = $zodiacArray[4]; break; case 5: $zodiac = $zodiacArray[5]; break; case 6: $zodiac = $zodiacArray[6]; break; case 7: $zodiac = $zodiacArray[7]; break; case 8: $zodiac = $zodiacArray[8]; break; case 9: $zodiac = $zodiacArray[9]; break; case 10: $zodiac = $zodiacArray[10]; break; case 11: $zodiac = $zodiacArray[11]; break; default: echo "<p>The Zodiac for this year has not been determined.</p>\n"; break; } //ends the switch statement echo "<p>You were born under the sign of the " . $zodiac . ".</p>\n"; echo '<img src="Images/' . $zodiac . '.jpg">'; echo "<p>You share a zodiac sign with President " . $zodiacArray["$zodiac"]["President"] . ".</p>\n"; echo "<p>You are person " . $year_count . " to enter " . $year . "</p>\n"; } //ends the else statement ?> </body> </html> I need to be able to use the "switch command" (Assignment requirement), to get the animal name based on the code, but it is not working... Help is appreciated! Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 25, 2014 Share Posted October 25, 2014 (edited) $zodiacArray is defined as an associative array. In the switch statement you are using numerical indexes. What you should be doing in the switch statement is setting $zodiac to the corresponding key for each case (the first case will set $zodiac to Rat, the second case will be Ox and so on) Example code. switch(($_POST['year'] - $start_year) % 6) { case 0: $zodiac = 'Rat'; break; case 1: $zodiac = 'Ox'; break; case 2: $zodiac = 'Tiger'; break; ... etc } $zodiac will be used later in your code as the key to return the president the user shares their zodiac sign with Edited October 25, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Barand Posted October 25, 2014 Share Posted October 25, 2014 ... and %6 will yield results between 0 and 5 only. You need %12 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.