Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Functions Ex. #4: Function for HTML

For this PHP exercise, first create an array called $months. Use the names of the months as keys, and the number of days for each month as values. For February, use the following for your value: "28 days, if leap year 29".

Next, write a function to create an option element for a form's select field. Make sure each option will be upper case. Both the array and the function should precede the HTML for the page.

Once again, you will be requesting user input. Create a form for the user with the request, "Please choose a month." Next, provide a select field with the months as options, looping through the array you created and using the function to create the option elements.

When the user clicks the submit button, return the statement "The month of $month has $number days.", where $month is the name of the month the user chose, and $number is the number of days. Be sure to include a different response for February.

 

Comments

Quick question, I set my function up like this:

function field($array){        
     foreach($array as $key => $value) {
        echo "<input type=\"radio\" name=\"month\" value=\"{$month}\"/>{$month}", br();
        echo "<input type=\"hidden\" name=\"days\" value=\"{$value}\"/>";
    }
}

in the array, i pulled in my $months array, and called it with

  1. <?php field($months); ?>

in the middle of my form. everything worked, and when i hit submit the keys carried over like they should have. my problem was with the values, every one of them was 31. did i do something wrong with the hidden field?

also the br(); is a function I made for an html break.
Thanks.

I was thinking the same way about the problem

had the same problem and ran into the other issue where the value for 'month' and 'days' were switched around in the echo statement.

If the original author is still monitoring these forums can you explain how the following function works in relation works?

Specifically how the function fills out the drop down (I understand the foreach portion which runs through the arrays) and supplies the number of days in the echo statement.

Many thanks.

wow my script is almost identical to the answer script!

I'm just wondering, why make another conditional statement for February? It works perfectly without it.

Thanks!

Two reasons why to include the conditional statement for Feb:
1. It's good practice.
2. The phrase sounds better without the second 'day'.

thanks for the tip. now that you mentioned it, it really sounds better that way. ^_^

Can someone tell me why the following syntax worked and the 2nd does not? seems the same to me.

THIS WORKS:

  1.   echo $month["February"];   // works fine with either "" or ''
  2.   echo "<p>The month of $get_month has ". $month["February"]. "days. </p>";   //worked
  3.   echo "<p>The month of $get_month has $month[$get_month] days. </p>";      // worked

BUT THIS SYNTAX DOES NOT WORK:
echo "The month of $get_month has $month["February"] days.";

Error Message: Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /Applications/MAMP/htdocs/functions_ex4.php on line 96

What's the reason?

Need help understanding the code with the function portion expanding out the drop down list and how it displays the number of days in the second echo statement.

What are you passing into the function as $str? I am presuming it's the $key array value but am foggy on where this is done?

I did not view the solution until i figure out whats causing my code not to work. My original code is almost the same as above but it did not work for me. I'm using PHP 5.4.16. I have to declare or access the variable as global for it to work. Here's my little code:

  1. <?php
  2. 	$monthList=array('January'=>'31','February' => '28 days, if leap year 29','March' => '30','April' => '30', 'May' => '31','June' => '30','July' => '31','August' => '31','September' => '30','October' => '31','November' => '30','December' => '31');
  3.  
  4. 	function createOptions(){		
  5. 		foreach($GLOBALS['monthList'] as $month => $days){
  6. 			echo "<option value=\"$month\">".strtoupper($month)."</option>";}
  7. 	}
  8. ?>
  9. <html>
  10. <head>
  11. <title>BitCraft - Ground Zero</title>
  12. </head>
  13. <body>
  14. 	<?php		
  15. 		if (isset($_POST['monthSelect'])){
  16. 			foreach($GLOBALS['monthList'] as $month => $days) {
  17. 				if($month == $_POST['monthSelect'])
  18. 					echo "The month of $month has $days days. <br/>";
  19. 			}
  20. 		}
  21. 		echo "Please choose a month: <br/>";
  22. 	?>
  23. 	<form action="<?php echo"{$_SERVER['PHP_SELF']}";?>" method="POST">		
  24. 		<select name="monthSelect" multiple="yes" size="6">
  25. 			<?php createOptions(); ?>
  26. 		</select>
  27. 		<br/><input type="submit" value="Check it!"/>
  28. 	</form>
  29. </body>
  30. </html>

This site has to be one of the best site I've seen for PHP tutorial. It's more engaging thus you'll have fun while taking on the little challenges. Retention of knowledge acquired is is more effective this way.
Thank you so much!