NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Creating Dropdown Selector for Date Selection

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 04-11-2007, 01:59 AM THREAD STARTER               #1 (permalink)
NamePros Member
Join Date: Feb 2007
Location: Surabaya - Indonesia
Posts: 27
silentwind is an unknown quantity at this point
 



Creating Dropdown Selector for Date Selection


taken directly from my blog.
Sometimes, we need to create a form that need a date selection. Here's how I handle this thing. First, let us create some function to do this :
PHP Code:
<?php
/**
 * Form Dropdown Month
 *
 * Creates dropdown element for selecting month
 *
 * @access  public
 * @param   string  the name of the form element
 * @param   string  the selected value
 * @param   string  some extra parameter
 * @return  string
 */ 
function form_dropdown_month($name ''$selected ''$extra '')
{
    if (
$extra != ''$extra ' '.$extra;

    
$form '<select name="'.$name.'"'.$extra.">\n";

    for(
$i 1$i <= 12$i++){

        
$start mktime(000$i1date('Y'));
        
$value date('n'$start);
        
$display date('F'$start);
        
$form .= '<option value="' $value '" ' . ($value === $selected 'selected' '') . '>' $display "</option>\n";

    }

    
$form .= "</select>\n";

    return 
$form;
}

/**
 * Form Dropdown Day
 *
 * Creates dropdown element for selecting day
 *
 * @access  public
 * @param   string  the name of the form element
 * @param   string  the selected value
 * @param   boolean leading zero value
 * @param   string  some extra parameter
 * @return  string
 */ 
function form_dropdown_day($name ''$selected ''$leadingZero false$extra '')
{
    if (
$extra != ''$extra ' '.$extra;

    
$form '<select name="'.$name.'"'.$extra.">\n";

    for(
$i 1$i <= 31$i++){

        
$value = ($leadingZero ? (strlen($i) == '0'.$i $i) : $i);
        
$display = ($leadingZero ? (strlen($i) == '0'.$i $i) : $i);
        
$form .= '<option value="' $value '" ' . ($value === $selected 'selected' '') . '>' $display "</option>\n";

    }

    
$form .= "</select>\n";

    return 
$form;
}

/**
 * Form Dropdown Year
 *
 * Creates dropdown element for selecting year
 *
 * @access  public
 * @param   string  the name of the form element
 * @param   string  the selected value
 * @param   string  some extra parameter
 * @return  string
 */ 
function form_dropdown_year($name ''$start$end$selected ''$extra '')
{
    if (
$extra != ''$extra ' '.$extra;

    
$form '<select name="'.$name.'"'.$extra.">\n";

    for(
$i $end$i >= $start$i--){

        
$form .= '<option value="' $i '" ' . ($i === $selected 'selected' '') . '>' $i "</option>\n";
????: NamePros.com http://www.namepros.com/code/315362-creating-dropdown-selector-for-date-selection.html

    }

    
$form .= "</select>\n";

    return 
$form;
}
?>
Now, here's how we use these function :
PHP Code:
<?php echo form_dropdown_month('month_start''1''id="month_start"'); ?> - 
<?php echo form_dropdown_day('day_start''1'true'id="day_start"'); ?> - 
????: NamePros.com http://www.namepros.com/showthread.php?t=315362
<?php echo form_dropdown_year('year_start'date('Y') - 50date('Y'), '''id="year_start"'); ?></td>
That's it. Now, we have simple function to handle date selection on form.
Like it? rep please.
__________________
-- Silent's Spot [blog]
-- Curriculum Vitae [CV]
silentwind is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 03:39 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger