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
https://forums.phpfreaks.com/topic/241330-default-value-in-a-drop-down-using-php/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.