Jump to content

Recommended Posts

i'm just playing around trying to learn new stuff.. and I wonder.. i cant get this code to work..

 

i tried using while, but it didnt output the months as 01,02,03 etc... only as December, january etc... and also the order of the months was all messed up.. was like it printed them out in random order... hehe...

 

but here's the code I've been trying now =)

  <select name="month">
        <option value="01" <?php if (date("m") == 01) { echo "selected=\"selected\""; } ?>>January</option>
        <option value="02" <?php if (date("m") == 02) { echo "selected=\"selected\""; } ?>>February</option>
        <option value="03" <?php if (date("m") == 03) { echo "selected=\"selected\""; } ?>>March</option>
        <option value="04" <?php if (date("m") == 04) { echo "selected=\"selected\""; } ?>>April</option>
        <option value="05" <?php if (date("m") == 05) { echo "selected=\"selected\""; } ?>>May</option>
        <option value="06" <?php if (date("m") == 06) { echo "selected=\"selected\""; } ?>>June</option>
        <option value="07" <?php if (date("m") == 07) { echo "selected=\"selected\""; } ?>>July</option>
        <option value="08" <?php if (date("m") == 08) { echo "selected=\"selected\""; } ?>>August</option>
        <option value="09" <?php if (date("m") == 09) { echo "selected=\"selected\""; } ?>>September</option>
        <option value="10" <?php if (date("m") == 10) { echo "selected=\"selected\""; } ?>>October</option>
        <option value="11" <?php if (date("m") == 11) { echo "selected=\"selected\""; } ?>>November</option>
        <option value="12" <?php if (date("m") == 12) { echo "selected=\"selected\""; } ?>>December</option>
      </select>

Link to comment
https://forums.phpfreaks.com/topic/46248-solved-will-thisone-ever-work/
Share on other sites

I'd use a for loop instead to create the month pull down menu.

 

Much better way of doing it:

<select name="month">
<?php

// do a loop here to create the month pull down menu
// this loop runs 12 times
// It will automatically output the html for us 
// and select the current month in list!
for($i = 0; $i <= 12; $i++)
{
    // set format of the number displayed for the month
    // this checks to see if $i is less than 10
    // if its it'll set $m to 0x (x being a number between 1 and 9)
    $m = ($i < 10) ? '0' . $i : $i;

    // start the html for the option tag
    $opt = '<option value="' . $m . '" ';

    // this bit checks to see if the month returned from the date() is equal to $m
    // if its equal then we extend the value of $opt to include the 
    // select attribute and close the opening option tag
    // else it will just close the option tag
    $opt .= (date('m') == $m) ? 'selected="selected">' : '>';

    // here we extend $opt further to show the month
    // and close the option tag.
    $opt .=  $m . "</option>\n";

    echo $opt;
}

?>
</select>

example HTML output:
<option value="01">01</option>
<option value="02" selected="selected">02</option>
...
<option value="12">12</option>

thanks man... it worked perfect.. but when i added date() and mktime() it got a lil messed up... it shows December, January, February etc to november... meaning that Dec is set as month 1 and Nov as month 12

 

		<?php

		for($i = 1; $i <= 12; $i++)
		{
			$m = ($i < 10) ? '0' . $i : $i;

			$opt = '<option value="' . $m . '" ';

			$opt .= (date('m') == $m) ? 'selected="selected">' : '>';

			$opt .=  date('F', mktime(0, 0, 0, $m, 0, 0))  . "</option>\n";

			echo $opt;
		}

		?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.