Jump to content

[SOLVED] auto-setting a select drop-down


HaLo2FrEeEk

Recommended Posts

Hey, I have 3 select menus for dates, I need to be able to auto set (on load) which number in the dropdown is selected, for example, if the date I pass to the page is this:

 

01/15/2007

 

I would use explode("/", $date) to get each number, I know for a fact that the format will always be (month)/(day)/(year), so no worries with that, but then how would I make it so that January is selected automatically, as is 15th, and the year 2007, in the drop-down menus?

 

Thank you.

Link to comment
Share on other sites

i can only think of this silly way:

 

<?php
// you get this from the 'explode'
$num = 5;
?>

<select id="s1">
<option value="1" <?php echo ($num == 1)?"selected":""; ?>>1</option>
<option value="2" <?php echo ($num == 2)?"selected":""; ?>>2</option>
<option value="3" <?php echo ($num == 3)?"selected":""; ?>>3</option>
<option value="4" <?php echo ($num == 4)?"selected":""; ?>>4</option>
<option value="5" <?php echo ($num == 5)?"selected":""; ?>>5</option>
<option value="6" <?php echo ($num == 6)?"selected":""; ?>>6</option>
<option value="7" <?php echo ($num == 7)?"selected":""; ?>>7</option>
<option value="8" <?php echo ($num == ?"selected":""; ?>>8</option>
<option value="9" <?php echo ($num == 9)?"selected":""; ?>>9</option>
</select>

Link to comment
Share on other sites

Trivial example. You can expand and dress it up to suit.

 

<?php
list($m,$d,$y) = explode("/",$_GET['date']);

echo "Month: <select name='month'>\n";
for ($mo=1;$mo<13;$mo++) {
    echo "<option value='". $mo. "'";
    if ($mo==$m) { echo " selected='selected'";}
    echo ">". $mo. "</option>\n";
}
echo "</select>";
?>

Link to comment
Share on other sites

Ok, well I messed around and figured out this (I decided to give up the date one, but I needed it for something else too):

 

echo "<select>";
$roles = array('Peon', 'Member', 'Staff', 'Overlord');
$role = "Member";
foreach($roles as $pos) {
  $sel = ($pos == $role) ? 'SELECTED' : '';
  echo "<option value=\"$pos\" $sel>$pos</option>\n";
  }
echo "</select>";

 

I'll see if I can do that with the date, somehow.  Thanks guys!

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.