current date into dropdown boxes on load

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

current date into dropdown boxes on load

Post by meander »

i have three dropdown boxes, one for month, one for day and one for year, and what i want it to do is when the page loads the boxes are selected on the current date, but then you can easily change it if needed. how would i go about doing this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Javascript or PHP. My vote: PHP.

have a look at the [php_man]date[/php_man]() function. you can use that in combination with ~dynamically selecting the proper dropdown option when it comes up.
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Post by meander »

oh yeah, php this is the php forum isnt it.... hehe jk. but ya in php. where would i find this date() function documentation?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

click the link... :arrow: [php_man]date[/php_man]()
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

to go a bit furthur i would do something like this

Code: Select all

<?
$selected_month = $_POST['month'];

$months = array_unique($selected_month, 'december','november','etc');

echo "<select name='select'>";

foreach ($months as $month)
{

echo '<option value="'.$month.'">$month</option>';

}

echo '</select>';

?>
edit.. ty for noticing quote fix
Last edited by John Cartwright on Sun Oct 10, 2004 6:36 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

note Phenom's post is a rough example.

it needs double quotes in the loop there, and fixing of the array sent to [php_man]array_unique[/php_man](). Although I don't see the point of using array_unique.
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Post by meander »

so then if i have the month box,

Code: Select all

<select name="select">
      <option value="1" selected>January</option>
      <option value="2">February</option>
      <option value="3">March</option>
      <option value="4">April</option>
      <option value="5">May</option>
      <option value="6">June</option>
      <option value="7">July</option>
      <option value="8">August</option>
      <option value="9">September</option>
      <option value="10">October</option>
      <option value="11">November</option>
      <option value="12">December</option>
    </select>
then how would i incorporate something like echo date("m") into that?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you missed the point,

you put everything in an array, and then the duplicate month (your current month) will be eliminated and the current month will be the one at the top of the list.


note, you have an error on your first option
Last edited by John Cartwright on Sun Oct 10, 2004 6:38 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use a loop, like Phenom's example, where you echo out each option checking if you need to output selected against the return from date().

Here's some threads you can stare at that involve "dynamic" selection:
[devnet]+dropdown +selected[/devnet]
User avatar
meander
Forum Commoner
Posts: 26
Joined: Sun Oct 10, 2004 3:09 pm

Post by meander »

Phenom wrote:you missed the point,
oh no i got it now but i didnt see your post till after i posted that
Post Reply