Results 1 to 8 of 8

Thread: date of birth form...

  1. #1
    439
    posts
    Registered User

    date of birth form...

    Hey guys, i've been looking on the forums and googleing for a php script that loops through years, months and days for options in a select input i.e.

    Code:
    <select name="year">
    <option value="1910">1910</option>
    <option value="1911">1911</option>
    <option value="1912">1912</option>
    <option value="1913">1913</option>
    <!--to current year-->
    <option value="2008">2008</option>
    </select>
    
    <select name="month">
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <!--etc...-->
    </select>
    
    <select name="days">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <!--etc...-->
    </select>
    I'm sure there is while or for loop that does this...just to save time lol. any info would be great. thanks guys
    while ( ! ( succeed = try() ) );

  2. #2
    got pwnt?

  3. #3
    Like for your year you would use.
    PHP Code:
    <?php
    for ($i 1910$i <= date('Y'); $i++) {
        echo 
    '<option value="'.$i.'">'.$i.'</option>';
    }
    or

    PHP Code:
    <?php
    for ($i 1910$i <= date('Y'); $i++) {
        echo 
    '<option value="{$i}">{$i}</option>';
    }
    //edit, Both above are the same but to be nice to your users:
    PHP Code:
    <?php
    for ($i 1910$i <= date('Y'); $i++) {
        
    $s date('Y') == $i 'selected="selected"' '';
        echo 
    '<option '.$s.' value="'.$i.'">'.$i.'</option>';
    }
    Last edited by Templarian; February 8th, 2008 at 01:58 PM.

    Join #kirupa | Click the under my Username | CL | Jeff

  4. #4
    I did these a while back
    PHP Code:
    <?php
        
    function yearOptions$endYear ''$startYear '1900' )
        {
            for ( 
    $i $startYear$i <= $endYear$i++ )
            {
                ( 
    $i == date('Y') ) ? $selected ' selected="selected"' $selected '';
                echo 
    '<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n";
            }
        }
        
        function 
    monthOptions()
        {
            
    $months = array( => "January""February""March""April""May""June""July""August""September""October""November""December" );
            
            foreach ( 
    $months as $monthNo => $month )
            {
                ( 
    $monthNo == date('n') ) ? $selected ' selected="selected"' $selected '';
                echo 
    '<option value="'.$monthNo.'"'.$selected.'>'.$monthNo.' - '.$month.'</option>'."\n";
            }
        }
        
        function 
    dayOptions()
        {
            for ( 
    $i 1$i <= 31$i++ )
            {
                ( 
    $i == date('j') ) ? $selected ' selected="selected"' $selected '';
                echo 
    '<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n";
            }
        }
    ?>
    very simple... i'm sure you can figure out how to use them
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  5. #5
    439
    posts
    Registered User
    ah yes, of course. cheers guys much appreciated.
    while ( ! ( succeed = try() ) );

  6. #6
    Quote Originally Posted by simplistik View Post
    very simple... i'm sure you can figure out how to use them
    Hey Simplistik, those functions are great, thanx...but cud you tell me how to print the form on the webpage. I tried the following, but nothing appeared on the browser.

    PHP Code:
    <?php function yearOptions(); ?>
    <?php 
    function dayOptions() ; ?>
    <?php 
    function dayOptions(); ?>
    PHP Code:
    <?php echo yearOptions(); ?>
    <?php 
    echo dayOptions() ; ?>
    <?php 
    echo dayOptions(); ?>
    Please help. Thanks much!

  7. #7
    Quote Originally Posted by chokra View Post
    Hey Simplistik, those functions are great, thanx...but cud you tell me how to print the form on the webpage. I tried the following, but nothing appeared on the browser.

    PHP Code:
    <?php function yearOptions(); ?>
    <?php 
    function dayOptions() ; ?>
    <?php 
    function dayOptions(); ?>
    PHP Code:
    <?php echo yearOptions(); ?>
    <?php 
    echo dayOptions() ; ?>
    <?php 
    echo dayOptions(); ?>
    Please help. Thanks much!
    You'll only use the function keyword when declaring a function, and the echo is contained within the function itself. So, you can just do this:

    PHP Code:
    <?php yearOptions(); ?>
    <?php monthOptions
    (); ?>
    <?php dayOptions
    (); ?>
    got pwnt?

  8. #8
    1,839
    posts
    Registered User
    think it would actually be
    <html>
    <select name="year">
    <?php yearOptions(); ?>
    </select>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012