Page 1 of 1

Ordering <option> values in PHP

Posted: Sun Nov 06, 2011 12:20 pm
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?

Re: Ordering <option> values in PHP

Posted: Sun Nov 06, 2011 12:36 pm
by internet-solution

Code: Select all

for($i=$endyear;$i>=$startyear;$i--)

Re: Ordering <option> values in PHP

Posted: Sun Nov 06, 2011 8:29 pm
by Pazuzu156
bah, why didn't I think of that. Thank you for the help. :D