Ordering <option> values in PHP

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
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Ordering <option> values in PHP

Post by Pazuzu156 »

In my registration form, I have a huge list of years. The list auto populates itself from the function. Example:

Code: Select all

<?php
function reg_date($name, $startyear, $endyear) {
    // Populate year drop menu
    $return = "<select id='year' name='".$name."year'>";
    $return .= "<option>YEAR</option>";
    for($i=$startyear;$i<=$endyear;$i++) {
        $return .= "<option value='".$i."'>".$i."</option>";
    }
    $return .= "</select>";
    return $return;
}


echo reg_date("name", 1900, 2011);
?>
When I populate it that way, it orders itself from 1900-2011 when i want it to order in a descending manor without ever using MySQL for it.

Any suggestions?
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Ordering <option> values in PHP

Post by internet-solution »

Code: Select all

for($i=$endyear;$i>=$startyear;$i--)
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Ordering <option> values in PHP

Post by Pazuzu156 »

bah, why didn't I think of that. Thank you for the help. :D
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply