BetaONE will rise again!

BetaONE will rise again! (http:\\b1.hcanet.com\forum/index.php)
-   Web Development (http:\\b1.hcanet.com\forum/forumdisplay.php?f=50)
-   -   Trouble With Date Ranges (http:\\b1.hcanet.com\forum/showthread.php?t=10299)

JacKDynne 5th Jan 04 03:20 PM

I am using this php code to generate a month/day/year pull down menu but I cannot set it to pull previous years from the pulldown, only the current year + . Any ideas on how to resolve this?

Here is the area in question:
Code:

// generate year drop-down
echo "<select name=" . $prefix . "y>";
        for ($x=$currYear; $x<($currYear+5); $x++)
        {
        $str = "<option value=$x";
 &nbsp;if ($x == $currYear)
 &nbsp;{
 &nbsp;$str .= " selected";
 &nbsp;}
        $str .= ">" . sprintf("%04d", $x) . "</option>";
        echo $str;
        }
echo "</select>";

Here is the full code:

Code:

// function to format DATE values
function fixDate($val)
{
// split it up into components
$datearr = explode("-", $val);
// create a timestamp with mktime(), format it with date()
return date("d M Y", mktime(0, 0, 0, $datearr[1], $datearr[2], $datearr[0]));
}

// generate three list boxes for d-m-y selection
function generateDateSelector($prefix="")
{
// month array
$monthArray = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

// get current year, month and date
$arr = getdate(mktime());
$currYear = $arr["year"];
$currMonth = $arr["mon"];
$currDay = $arr["mday"];

// generate date drop-down
echo "<select name=" . $prefix . "d>";
        for ($x=1; $x<=31; $x++)
        {
        $str = "<option value=" . sprintf("%02d", $x) . "";
 &nbsp;if ($x == $currDay)
 &nbsp;{
 &nbsp;$str .= " selected";
 &nbsp;}
        $str .= ">" . sprintf("%02d", $x) . "</option>";
        echo $str;
        }
echo "</select>";

// generate month drop-down
echo "<select name=" . $prefix . "m>";
        for ($x=1; $x<=12; $x++)
        {
        $str = "<option value=" . sprintf("%02d", $x) . "";
 &nbsp;if ($x == $currMonth)
 &nbsp;{
 &nbsp;$str .= " selected";
 &nbsp;}
        $str .= ">" . $monthArray[$x] . "</option>";
        echo $str;
        }
echo "</select>";

// generate year drop-down
echo "<select name=" . $prefix . "y>";
        for ($x=$currYear; $x<($currYear+5); $x++)
        {
        $str = "<option value=$x";
 &nbsp;if ($x == $currYear)
 &nbsp;{
 &nbsp;$str .= " selected";
 &nbsp;}
        $str .= ">" . sprintf("%04d", $x) . "</option>";
        echo $str;
        }
echo "</select>";
}

?>

I tried changing this: $x<($currYear+5); to this:$x<($currYear-5); and some other variations but that does not seem to work. Any ideas while I keep researching?

Many TIA's to any who reply :)

/JD

SlickVic78 5th Jan 04 06:14 PM

Hey JacKDynne,

So your code right now will post from this year and up, not past years correct?

The issue falls within this statement:

Code:

for ($x=$currYear; $x<($currYear+5); $x++)
You have set $x to equal the current year, you set your limit to have $x be less than 5 years after the current year, and you set your counter to always increment by 1.

If you were to set the limit to look like this $x<($currYear-5), this still doesn't work (as you already noticed), because the current year has already succeeded the limit (being you are comparing the current year,($x, with being less than 5 years before the current year) which makes this statement already false, therefore, no years should have been posted when you have tried that out...

I think what you can do is this, to get at least 5 years before the current year:

Code:

for ($x=$currYear-5; $x<($currYear+5); $x++)
This way you set $x to being 5 years before the current year and the limit should be 5 years after the current year.

See if this helps, if not, let me know and I'll look into it further...

-SlickVic78

JacKDynne 5th Jan 04 07:48 PM

You rock SlickVic :)

That was just about the only variation I did not try :lol:

Thanks for the help m8 - I appreciate it :)

/JD


All times are GMT +1. The time now is 11:10 AM.

Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.