Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello, April,

 

I think I can try and help you here.

 

Make the function in menus.php take arguments to indicate the starting year and the number of years to generate. Make the later argument have a default value. Then rewrite the function body so that it uses these values in the year for loop.

 

In the function as it is written on page 262, the year menu starts with the current year:

$start_year = date('Y');

 

So, instead of that, you'll rewrite the function so that it takes a first argument allowing the user to choose the starting year (any year).

 

Then, still on page 262, the number of years to generate is hard-coded within the function: it's 10 years:

for ($y = $start_year; $y <= ($start_year + 10); $y++) 

So your new function will have a second argument allowing the user to choose the number of years the function must generate. And you will include a default value for this second argument.

 

I hope this helps,

Link to comment
Share on other sites

Bear with me here, but when you say arguments, my mind goes back to line item number 13 on page 261 and see that their aren't arguments in the specific function make_date_menus - so when you say "change the arguments" what line in the code are you referring to?

 

Will the end result even be different, because as it stands in the book and when I run the code as the book states, I can choose the year I want... So what is he asking us to change?

 

<?php //Script 10.1 Menus.php

 

//Array to store the months: index starting at 1 to make sure January is starting at 1 not 0

 

function make_date_menus(){

$months=array (1=>'January','February','March','April','May','June','July','August','September','October','November','December');

 

// Make the month pull-down menu:

print'<select name="month">';

foreach ($months as $key => $value){

print"\n<option value=\"$key\">$value</option>";

}

print"</select>";

 

// Make the day pull-down menu:

print'<select name="day">';

for($day =1; $day<=31; $day++){

print"\n<option value=\"$day\">$day</option>";

}

 

print"</select>";

 

// Make the year pull-down menu:

print'<select name="year">';

$start_year = date('Y');

for($y=$start_year; $y<=($start_year +10); $y++){

print"\n<option value=\"$y\">$y</option>";

}

print'</select>';

 

}// End of make_date_menus() function.

 

// Make the form:

print'<form action="" method="post">';

make_date_menus();

print'</form>';

?>

Link to comment
Share on other sites

Hello, April,

 

I didn't write "change the arguments". I wrote: "rewrite the function so that it takes arguments".

 

Right now, if you use this function in your website, what the website-user sees is this:

date_m10.jpg

 

He/She has no way of choosing when the calendar starts. Now, imagine you want to allow the website-users to create their own calendar, starting in 1675 and running for 20 years, or just as well starting in 2005 and displaying 5 years only. You have to create a form to allow them to state the first year and the number of years for the menu, register these values in your script, and use them as arguments for your make_date_menus() function:

 

calend10.jpg

So you have to ADD arguments to the function; rewrite it so that it takes arguments: you will no longer hard-code the values in the for loop you are using for years, but use the values provided by the web-user.

 

And, since that's part of the "Pursue" requirements, you'll also provide your function with a default value for the second argument (the number of years).

 

I hope this helps,

  • Upvote 1
Link to comment
Share on other sites

Well - here's what I've got! I actually figured out how to make the form - but I need some help determining my next steps to making the form interact with my functions...

 

<?php //Script 10.1 Menus.php

 

//make the new form that will input user selection

function make_form_input($name, $label) {

 

// Begin a paragraph and a label:

print '<p><label>' . $label . ': ';

 

// Begin the input:

print '<input type="text" name="' . $name . '" ';

 

// Add the value:

if (isset($_POST[$name])) {

print ' value="' . htmlspecialchars($_POST[$name]) . '"';

}

 

 

// Complete the input, the label and the paragraph:

print ' /></label></p>';

 

} // End of make_text_input() function.

 

// Make the form:

print '<form action="" method="post">';

 

// Create some text inputs:

make_form_input('first_year', 'Choose the first year in the menu');

make_form_input('year_count', 'How many years would you like it to generate?');

 

 

 

print '<input type="submit" name="submit" value="Submit!" /></form>';

 

print '<br />';

 

 

//Array to store the months: index starting at 1 to make sure January is starting at 1 not 0

 

function make_date_menus(){

$months=array (1=>'January','February','March','April','May','June','July','August','September','October','November','December');

 

 

 

// Make the month pull-down menu:

print'<select name="month">';

foreach ($months as $key => $value){

print"\n<option value=\"$key\">$value</option>";

}

print"</select>";

 

// Make the day pull-down menu:

print'<select name="day">';

for($day =1; $day<=31; $day++){

print"\n<option value=\"$day\">$day</option>";

}

 

print"</select>";

 

// Make the year pull-down menu:

print'<select name="year">';

$start_year = date('Y');

for($y=$start_year; $y<=($start_year +10); $y++){

print"\n<option value=\"$y\">$y</option>";

}

print'</select>';

 

}// End of make_date_menus() function.

 

// Make the form:

print'<form action="" method="post">';

make_date_menus();

print'</form>';

?>

Link to comment
Share on other sites

Kudos for figuring out how to use the make_form_input() function. But just for now, let's forget about this function and create the form the usual traditional way, so as to keep things simple for the make_date_menus() function.

 

As it stands now, the make_date_menus() is great if you always want your calendar to start with the current year and to generate 10 years. But if you want web-users to be able to create their own calendar, you need to get the values they want to use for the first year and for the year count. That's why you provide them with a form like this:

 

<form action="" method="post">

<p>

<label for="first_year">Choose the first year in the menu</label> <input type="text" name="first_year" size="6" />

</p>

<p>

<label for="year_count">How many years would you like it to generate?</label> <input type="text" name="year_count" size="6" />

</p>

<p>

<input type="submit" name="submit" value="Submit!" />

</p>

</form>

 

Once the form has been submitted, you get an array of values, $_POST, containing:

$_POST['first_year']

$_POST['year_count']

Their values will be the data entered by your web-user, for instance "2005" for $_POST['first_year'] and "5" for $_POST['year_count'].

 

In a real-world situation, you would need to validate this data, but for now we'll just check that the user entered data and assign these values to new variables with shorter names:

if (isset($_POST['first_year']))

{

$first_year = $_POST['first_year'];

}

if (isset($_POST['year_count']))

{

$year_count = $_POST['year_count'];

}

 

Now, these are the two variables you want to use as arguments for your make_date_menus() function, so that the calendar starts with the value assigned to $first_year, and it generates the number of years assigned to $year_count. That means you will no longer use date('Y') for the first year, since you want to use the data provided by the web-user; and you will no longer use "10" in the for loop, since here too you want to use the data provided by the web-user.

 

You just have to reconcile this with what Larry explains on page 265 and following:

The input a function takes is called an argument (or a parameter). The syntax for writing functions that take arguments is as follows:

function function_name($arg1, $arg2, …){

statement(s);

}

The function’s arguments are in the form of variables that are assigned the values sent to the function when you call it. The variables are defined using the same naming rules as any other variable in PHP:

function make_full_name($first, $last) {

print $first . ' ' . $last;

}

Functions that take input are called much like those that don’t—you just need to remember to pass along the necessary

values. You can do this by passing variables:

make_full_name($fn, $ln);

 

I hope this helps,

  • Upvote 1
Link to comment
Share on other sites

To answer this question, it really is quite simple although I did take quite a while figuring it out:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>Date Menus</title>

</head>

<body>

<?php // Script 10.1 - menus.php

/* This script defines and calls a function. */

 

// This function makes three pull-down menus for the months, days, and years.

function make_date_menus($n, $v = 10) {

 

// Array to store the months:

$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

 

// Make the month pull-down menu:

print '<select name="month">';

foreach ($months as $key => $value) {

print "\n<option value=\"$key\">$value</option>";

}

print '</select>';

 

// Make the day pull-down menu:

print '<select name="day">';

for ($day = 1; $day <= 31; $day++) {

print "\n<option value=\"$day\">$day</option>";

}

print '</select>';

 

// Make the year pull-down menu:

print '<select name="year">';

for ($y = $n; $y <= ($n + $v); $y++) {

print "\n<option value=\"$y\">$y</option>";

}

print '</select>';

 

} // End of make_date_menus() function.

 

// Make the form:

print '<form action="" method="post">';

make_date_menus('1980', '20');

print '</form>';

 

?>

</body>

</html>

 

The above would then place 1980 as the year and 20 subsequent years in the drop-down box. It also has a default value of 10 years.

 

If I wanted to pass the function variables instead of literal values i guess I'd use another script with a form asking for the year and current year in a form then pass the variables to the make_date_menus function?

  • Upvote 1
Link to comment
Share on other sites

AprilSwenby

function make_date_menus(){
$months=array (1=>'January','February','March','April','May','June','July','August','September','October','November','December');
...
}
print'<form action="" method="post">';
make_date_menus();
print'</form>';

 

phpRob

function make_date_menus($n, $v = 10) {
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
...
print '<select name="year">';
for ($y = $n; $y <= ($n + $v); $y++) {
	print "\n<option value=\"$y\">$y</option>";
}
print '</select>';
}
print '<form action="" method="post">';
make_date_menus('1980', '20');
print '</form>';

 

Shouldn't you both be putting something in the quotes for the action attribute?

 

I recommend taking the $months array assignment out of the function, the array is being reassigned each time the function is called. Even though the function is only called once, if it was called multiple times per page, you would be reassigning over and over, even though months do not change.

 

AprilSwenby, please use spaces to separate values of an array when assigning. It is not required, but it makes code look so much more nicer, especially if someone else is going to read it. Use spaces. Like $months=array could use some spaces. This forum, fortunately, also has a feature that allows you to paste your code and let others read it with highlighted syntax and stuff. To utilise it, look to the editor bar for two less than and equal to signs (< >), or use '[' CODE ']' to open and '[' /CODE ']' (without the quotes) to close your code. The code will then be formatted.

Link to comment
Share on other sites

 

Shouldn't you both be putting something in the quotes for the action attribute?

 

 

This attribute is optional, if left blank it will use the document address. I guess it is good prcatice to use the script name in the action attribute even though it's used on the same page.

 

 

I recommend taking the $months array assignment out of the function, the array is being reassigned each time the function is called. Even though the function is only called once, if it was called multiple times per page, you would be reassigning over and over, even though months do not change.

 

 

I'm just following Larry's code so he'd be the best one to answer this question. You do make a valid point though.

Link to comment
Share on other sites

Yes, if you were to use the months more than once per page it would be better to create the array once.

 

Lingolatz, keep in mind that you're looking at code used in a book to demonstrate a specific idea (here: creating a function that takes arguments). Whenever that's the case, there are going to be immaterial aspects that would be changed in a different context, such as providing an action attribute or making the months array reusable.

Link to comment
Share on other sites

 Share

×
×
  • Create New...