-
1. Re: setting dynamic default values in a list/menu
bregent Feb 6, 2012 1:56 PM (in response to whatalotofrubbish)In ASP/VBScript you would:
<option value=<%=DatePart("yyyy",Now())%> selected><%=DatePart("yyyy",Now())%></option>
In PHP, you could use the getdate() function to return the current date in an array and reference the year using the [year] subscript.
-
2. Re: setting dynamic default values in a list/menu
whatalotofrubbish Feb 7, 2012 3:51 AM (in response to bregent)That's a good idea. I filled my date options from a snippet, and never thought of typing the code into the options box.
Will work on it.
Thanks
Howard Walker
-
3. Re: setting dynamic default values in a list/menu
David_Powers Feb 7, 2012 5:26 AM (in response to whatalotofrubbish)<option value="2012" <?php if (date('Y') == '2012') echo 'selected'; ?>>2012</option> -
4. Re: setting dynamic default values in a list/menu
whatalotofrubbish Feb 7, 2012 8:12 AM (in response to bregent)This is what I came up with.
<?php $thedate = getdate();
$d=$thedate[mday];
$m=$thedate[mon];
$y=$thedate[year];
echo "Today's Date: ". $d .'/' .$m. '/'. $y;
?>
/* the form and the rest of the select items not shown */
<option value="<?php echo($m)
?>" selected>Current Month</option>
Seems to work with no problems, though the first time I tried it, it returned the current month + 1.
Since then I remembered to put <selected> in the code and now it works every time.
Put it in position 1 in the list, else the list looks odd.
Message was edited by: Howard Walker Fixed a small problem.
-
5. Re: setting dynamic default values in a list/menu
bregent Feb 7, 2012 12:13 PM (in response to whatalotofrubbish)Take a look at Davids solution too. His would eliminate duplicate values from appearing in the list (the static value plus the dynamic one). Duplicates shouldn't cause a problem, however.
You can do the same for the rest of the fields, using the condition test for each possible value.
-
6. Re: setting dynamic default values in a list/menu
whatalotofrubbish Feb 9, 2012 4:40 AM (in response to bregent)I had another look and must agree that David's idea is a lot more elegent than the way I did it. Bit more code, but it does get rid of the duplicate values in each list.
Will use that one. Thanks to you both.
-
7. Re: setting dynamic default values in a list/menu
whatalotofrubbish Mar 18, 2012 3:20 PM (in response to David_Powers)<option value="2012" <?php if (date('Y') == '2012') echo 'selected'; ?>>2012</option>The above solution works well for that item.
I now have another one which may help others.
I populate a drop down list with the values 1 to 12, with the default being set to 6 and insert a record.
This represents the number of months that the record will be displayed until it is deleted.
When I pull the record back from the database to update it, I need to show the current value of the field, which is stored in $row_eventset['event_expiry'] ready for it to be updated.
Code as follows:
<select name="event_expiry" size="1" tabindex="5" title="select from the list">
<?php for ($i = 1; $i <= 12; $i++) { // loop through the months
echo "<option value= '$i' " ;
if ($i == $row_eventset['event_expiry']) { //check if this is the same as the record being edited
echo 'selected="selected" ' ; // if so select it
}
echo ">$i</option <br>"; // the <br> is essential in a browser, though it works without it in live view.
}
?>
</select>
I hope that it saves someone some time.
When I first tried this, it worked fine in live view. However in a browser, all the months appeared on the same line. I had to insert the <br> command to make it work properly.




