/    Sign up×
Community /Pin to ProfileBookmark

Building Select options

Hey guys, I wrote the following functions to give me 3 drop downs, one for month one for year and one for day. I made them so they can take an input if the form being printed is for editing a data set, to make the date from the database be the default selection for the drop downs. I was just wondering where it might be improved upon, it works exactly as I want it to, just not sure how/if it can be made better.

[code=php]
function monthSelect($fieldname,$selected = 0) {
$month = array(‘January’
,’February’
,’March’
,’April’
,’May’
,’June’
,’July’
,’August’
,’September’
,’October’
,’November’
,’December’
); //used to create month drop down list
$select = “<select name=”{$fieldname}” id=”{$fieldname}” size=”1″>
<option selected=”selected”>Month</option>n”;
for( $i=0;$i<=11;$i++) {
$value = $i+1;
//Use month array to create month options
if( $selected == $value ) {
// If the set month is the current option, make it selected
$select .= “<option value=”{$value}” selected>$month[$i]</option>n”;
} else {
// else its not selected
$select .= “<option value=”{$value}”>{$month[$i]}</option>n”;
}
}
$select .= ‘</select>’; // close select
return $select;
}
function daySelect($fieldname,$selected = 0) {
// Prints 31 days
$select = “<select name=”{$fieldname}” id=”{$fieldname}” size=”1″>
<option selected=”selected”>Day</option>n”;
for( $i=1; $i<32; $i++ ) {
if( $selected == $i ) {
// If the set day is the one being printed, make it selected
$select .= “<option value=”{$i}” selected>{$i}</option>n”;
} else {
// else its not selected
$select .= “<option value=”{$i}”>{$i}</option>n”;
}
}
$select .= “</select>”; // close the select
return $select;
}
function yearSelect($fieldname,$selected = 0) {
// Prints 25 years starting 1 year in the future
$select = “<select name=”{$fieldname}” id=”{$fieldname}” size=”1″>
<option selected=”selected”>Year</option>n”;
for( $i=-1; $i<25; $i++ ) {
$year = date(“Y”)-$i;
if( $year == $selected ) {
// If the set year is the current option, make it selected
$select .= “<option value=”{$year}” selected>{$year}</option>n”;
} else {
// else its not selected
$select .= “<option value=”{$year}”>{$year}</option>n”;
}
}
$select .= “</select>”; // close select
return $select;
}[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@mikeroqApr 15.2011 — Few tips:

Store date in your DB as a timestamp, use strtotime to convert it into a timestamp and date to take it from a timestamp.

Have the days selector update with JS based on the month to show the correct number of days.

Allow manual year input? I don't know the purpose of your script so you might just want a certain range of years.
Copy linkTweet thisAlerts:
@NoasITauthorApr 15.2011 — Only need a limited range of values for year.

These functions have nothing to do with how the information is stored in the database.

I don't know JS... well at least not enough to do anything useful.
Copy linkTweet thisAlerts:
@mikeroqApr 15.2011 — You would want to use AJAX to accomplish the change part.

AJAX would call a php file using your function to set the amount of days for the selected month.
[code=php]
<?php
function daySelect($fieldname,$date,$selected = 0) {

$select = "<select name="{$fieldname}" id="{$fieldname}" size="1">
<option selected="selected">Day</option>n";
$num_days = date("t",strtotime('-1 month',$date));
for( $i=1; $i<$num_days; $i++ ) {
if( $selected == $i ) {
// If the set day is the one being printed, make it selected
$select .= "<option value="{$i}" selected>{$i}</option>n";
} else {
// else its not selected
$select .= "<option value="{$i}">{$i}</option>n";
}
}
$select .= "</select>"; // close the select
return $select;
}
?>
[/code]


Of course you would have to have the user select the month and the year first for leap year support. Otherwise you would do something like this:
[code=php]
<?php
function daySelect($fieldname,$month,$selected = 0) {

$select = "<select name="{$fieldname}" id="{$fieldname}" size="1">
<option selected="selected">Day</option>n";
if ($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12)
{
$num_days = '31';
}
else if ($month == 2)
{
$num_days = '29';
// added one for leap years
}
else
{
$num_days = '30';
}
for( $i=1; $i<$num_days; $i++ ) {
if( $selected == $i ) {
// If the set day is the one being printed, make it selected
$select .= "<option value="{$i}" selected>{$i}</option>n";
} else {
// else its not selected
$select .= "<option value="{$i}">{$i}</option>n";
}
}
$select .= "</select>"; // close the select
return $select;
}
?>[/code]


I'm not quite sure on the intended purpose of your functions for their role in your script, adding this functionality might not be necessary, or there could be a completely different approach via an alternate method of choosing the date.
Copy linkTweet thisAlerts:
@hastxApr 15.2011 — I like usning one single function to handle the creation of all option lists...with a little mod I'm sure this would do everything you need it to. This would allow you to define the actual select element within the HTML code.

[code=php]
function array2Opts($array,$selected) {
$opts = "";
foreach( $array as $arr) {
$sel_value = ($arr == $selected) ? " selected="selected"" : "";
$opts .= "<option value="$arr"$sel_value>$arr</option>n";

}
return $opts;
}

//define array
$month = array('January'
,'February'
,'March'
,'April'
,'May'
,'June'
,'July'
,'August'
,'September'
,'October'
,'November'
,'December'
); //used to create month drop down list

//get opts from array
$MonthOpts = array2Opts($month,$selected);
[/code]


[code=html]
<select name="month">
<?php
echo $MonthOpts;
?>
</select>
[/code]
×

Success!

Help @NoasIT spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...