Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

setting dynamic default values in a list/menu

Engaged ,
Feb 06, 2012 Feb 06, 2012

Copy link to clipboard

Copied

To save problems with inserting dates into a Mysql database, I have split the input fields on the file insert form into three drop down lists containing the day, month and year.

In order to make it easy for users, I wish to take the current date, split it into day (dd) month (mm) and year (yyyy), and use one of these values as the default option for each of the three fields.

In html you would use :

<option value="2012" selected>2012</option> for the default, but this is a fixed default.

I want it to change depending on the actual year., so that if today is 21-02-2012, (thats how we use dates in the UK)  the three fields default to 21 (dd) 02 (mm)  2012 (yyyy).

I can re-format them into MySql date format mmddyyyy no problem later to insert them into the database

Any ideas?

Or is there a better way to do this?

TOPICS
Server side applications

Views

2.5K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 06, 2012 Feb 06, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 07, 2012 Feb 07, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 07, 2012 Feb 07, 2012

Copy link to clipboard

Copied

<option value="2012" <?php if (date('Y') == '2012') echo 'selected'; ?>>2012</option>

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 18, 2012 Mar 18, 2012

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 07, 2012 Feb 07, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 07, 2012 Feb 07, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 09, 2012 Feb 09, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines