daxguy Posted July 7, 2011 Share Posted July 7, 2011 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 More sharing options...
AbraCadaver Posted July 7, 2011 Share Posted July 7, 2011 Should work: <select name="month"> <?php for($i=1; $i<=12; $i++) { $selected = (date('m') == $i) ? "selected='selected'" : ""; echo "<option $selected value='$i'>$i</option>"; } ?> </select> Link to comment https://forums.phpfreaks.com/topic/241330-default-value-in-a-drop-down-using-php/#findComment-1239643 Share on other sites More sharing options...
daxguy Posted July 7, 2011 Author Share Posted July 7, 2011 Thanx alot it works.. but i do not get how the logic works.. would you mind explaining it a bit? Link to comment https://forums.phpfreaks.com/topic/241330-default-value-in-a-drop-down-using-php/#findComment-1239647 Share on other sites More sharing options...
TeNDoLLA Posted July 7, 2011 Share Posted July 7, 2011 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 https://forums.phpfreaks.com/topic/241330-default-value-in-a-drop-down-using-php/#findComment-1239651 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.