SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    May 2002
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Working with Months in a drop-down list

    I'm writing a very simple calender/BB system. One of the features I would like the entry form to have, is a drop-down list of the months of the year...so the user will not have to type it in.

    Is there any way to have the current month selected by default? In other words, if the page were to load right now, May would already be selected in the list...any ideas?

    Thank you, thank you!

  2. #2
    SitePoint Evangelist azizur_rahman's Avatar
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What Server site Language are you using?

    well it does not matter

    what you need to so do is:

    <option value="4" selected >4</option>

    get the month and add that "selected" keyword for it... thats it.

    this will select the option as being the default value.
    Azizur Rahman
    Web Application Developer

  3. #3
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this isnt very good but someone will come up with somethin better..

    PHP Code:
    <?
    echo "<select>";
    $d date(M);
    echo 
    "<option selected>$d</option>";
    $months = array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    for (
    $a=0$a count($months); $a++) {
    echo 
    "<option>$months[$a]</option>";
    }
    echo 
    "</select>";
    ?>
    http://redgoals.com ... my site ... nuff said

  4. #4
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    red, not bad...here's my slight modification to your code:
    PHP Code:
    <?
    $d
    =date(M);
    $months = array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    echo 
    "<select name=\"whatever_you_want_to_name_this\">";
    foreach (
    $months as $month) {
        echo 
    "<option";
        if(
    $month==$d) {
            echo 
    " selected";
        }
        echo 
    ">$month</option>";
    }
    echo 
    "</select>";
    ?>
    having the code for the "selected" bit inside avoids that the current months is repeated twice (at the top of the select, and then later on in the list of months)
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  5. #5
    SitePoint Member
    Join Date
    Jan 2002
    Location
    Kentucky
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A drop down menu of the months... easy:

    PHP Code:
    <?php

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

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

    for (
    $x 1$x <= 12$x++) {
        echo 
    "\n\t" '<option value="' $x '">' $m_names[$x] . '</option>';
    }

    echo 
    "\n" '</select>';

    ?>

  6. #6
    SitePoint Zealot infoxicated's Avatar
    Join Date
    Jun 2001
    Location
    UK
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah - that was easy, but it doesn't do what he asked for!

    PHP Code:
    <?php 
    $date 
    gmdate("Ymd"); 
    $day substr($date,6,2); 
    $month substr($date,4,2); 
    $year substr($date,0,4); 
    print 
    "<select name=\"day\">"
    $n=1
    while(
    $n<=31) { print "<option"
       if (
    $n==$day) {
       print 
    " selected"; } 
       if (
    $n<10) { print ">0$n</option>"; } 
       else { print 
    ">$n</option>"; }
       
    $n++; }
    print 
    "</select>"; print " / "

    print 
    "<select name=\"month\">";
    $n=1;
    while(
    $n<=12) { print "<option"
       if (
    $n==$month) { print " selected"; } 
       if (
    $n<10) {  print ">0$n</option>"; } else { 
       print 
    ">$n</option>"; } 
       
    $n++; } 
    print 
    "</select>"; print " / "

    print 
    "<select name=\"year\">"
    $n=2002;
    while(
    $n<=2005) { print "<option"
       if (
    $n==$year) { print " selected"; }
       print 
    ">$n</option>"
       
    $n++; } 
    print 
    "</select>"?>
    Try that - does a drop down list with the current day, month and year selected.

  7. #7
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by infoxicated
    Yeah - that was easy, but it doesn't do what he asked for!

    [...]

    Try that - does a drop down list with the current day, month and year selected.
    he didn't ask for the current day and year either
    my code does all he asked for...
    just teasing of course...
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  8. #8
    SitePoint Zealot infoxicated's Avatar
    Join Date
    Jun 2001
    Location
    UK
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Yeah, me too!

    *caught showing off*

  9. #9
    SitePoint Member
    Join Date
    May 2002
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    very impressive, everyone

    thanks a lot...

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you really want to do it right use a combo of javascript and PHP to create drop down lists whose days chnage according to the month. Try chaning the month to Feb you'll notice 28 days in the day list, and March will have 31. Have fun!

    PHP Code:
    <html>
    <head>
        <title>Untitled</title>
    </head>

    <body onLoad="fill_init();">
    <SCRIPT language="JavaScript">
    <!--
    <?php
    //set initial variables which can come from form or database or wherever
    $month date("m");
    $day =  date("d");
    $year =  date("Y");
    $numDays date("t"mktime(0,0,0,$month,$day,$year));



        
    //First Year in list
        
    $startYear 2002;
        
    //Number of years to include
        
    $numYears 10;
        for(
    $i=$startYear;$i<$startYear+$numYears;$i++) {
    ?>
    var months<?php echo $i?> = new Array(12)
    <?php
        
    for($j=1;$j<=12;$j++) {
            print 
    'months'.$i.'['.$j.'] = '.date("t"mktime(0,0,0,$j,1,$i)).'; ';
            }
        print 
    "\n";
        }
    ?>
    var month = "";
    var days = "";
    var year = "";
    var thisyear = "";
    //-->
    </SCRIPT>
    <form>
    <b>Month</b><br><select name="month" onChange="fillDays(); ">
    <?php
        
    for($i=1;$i<=12;$i++) {
    ?>
            <option value="<?php echo $i?><?php echo ($i == $month) ? ' selected' ''?>><?php echo $i?></option>
    <?php
            
    }
    ?>
    </select>
    <br>
    <b>Day</b><br><select name="days">
    <?php
        
    for($i=1;$i<=$numDays;$i++) {
    ?>
            <option value="<?php echo $i?><?php echo ($i == $day) ? ' selected' ''?>><?php echo $i?></option>
    <?php
            
    }
    ?>
    </select>
    <br><b>Year</b><br><select name="year" onChange="fillDays(); ">
    <?php
        
    for($i=$startYear;$i<$startYear+$numYears;$i++) {
    ?>
            <option value="<?php echo $i?><?php echo ($i == $year) ? ' selected' ''?>><?php echo $i?></option>
    <?php
            
    }
    ?>
    </select>
    </form>
    <script language="JavaScript">
    <!--
    function fill_init() {
        month = document.forms[0].month;
        days = document.forms[0].days;
        year = document.forms[0].year;
        }
    function getYear() {
        for(var i=0; i<year.options.length; i++) {
            if (year.options[i].selected == true) {
                thisyear = eval('months' + year.options[i].value);
                }
            }
        }
    function getDays() {
        for(var i=0; i<month.options.length; i++) {
            if (month.options[i].selected == true) {
                return thisyear[month.options[i].value];
                }
            }
        }
    function clearDays() {
        days.options.length = 0;
        }
    function fillDays() {
        getYear();
        var numdays    = getDays();
        clearDays();
        for(var j=1; j<=numdays; j++) {
            var opt = "";
            opt = new Option(j,j);
            days.options[days.options.length] = opt;
            opt = "";
            }
        days.options[0].selected = true;
        }
    //-->
    </script>


    </body>
    </html>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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