Jump to content

Default Value in a drop down using PHP?


daxguy

Recommended Posts

i have the folowing code..

<select name="month">

<?php

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

{

 

echo "<option value='$i'>$i</option>";

 

}

?>

</select>

 

in the above code what is happening is a simple display of 1 to 12 months in a drop menu.. i am using the date function to get the month

 

date('m');

 

what i want is that the drop down should automatically focus on month 7 as this is the 7th month.. The selection of the month from the drop down should automatically move to the current month.. and if the user wants to change the month he can change the month he wants to.. right now it displays a list focussing on 1 as 1 is the starting value in the loop..

i hope you understand. i am attaching a thumbnail of what i have in the results.. all i want is 1 to be changed by the current month with the other digits being displayed on drop down!

 

[attachment deleted by admin]

Link to comment
Share on other sites

It is the short IF-clause

 

this line

$selected = (date('m') == $i) ? "selected='selected'" : "";

 

is equivalent to this

if (date('m') == $i)
{
	$selected = "selected='selected'";
}
else
{
	$selected = "";
}

 

syntax: (condition) ? value_if_true : value_if_false

Link to comment
Share on other sites

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.