Colin Brazier
asked on
Eliminate duplication here
Obvious duplication here, just because I need an ascending date range in one, and descending in the other. But I can't think how to do it with one version only.
function DateDropDown($startdays,$size,$DateDropName,$SatSun)
{
// $startdays = for results, we start at present day (0), for fixtures, we start at next week(7).
// $size = the number of days to display in the drop down
echo "<select class='formSelect' id='$DateDropName' name='$DateDropName' >\n";
for ($i = $startdays; [b]$i >= $size; $i--[/b])
{
$theday = mktime (0,0,0,date("m") ,date("d")+$i ,date("Y"));
$option=date("D M j, Y",$theday);
$value=date("Y-m-d",$theday);
echo "<option value=\"$value\">$option</option>\n";
}
echo "</select>\n";
return;
}
function DateDropDownFix($startdays,$size,$DateDropName,$SatSun)
{
// $startdays = for results, we start at present day (0), for fixtures, we start at next week(7).
// $size = the number of days to display in the drop down
echo "<select class='formSelect' id='$DateDropName' name='$DateDropName' >\n";
for ($i = $startdays; [b]$i <= $size; $i++[/b])
{
$theday = mktime (0,0,0,date("m") ,date("d")+$i ,date("Y"));
$option=date("D M j, Y",$theday);
$value=date("Y-m-d",$theday);
echo "<option value=\"$value\">$option</option>\n";
}
echo "</select>\n";
return;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Chris, will check it out tomorrow.
ASKER
and Norie!
ASKER
Thanks guys. Especially Chris who introduced me to some different concepts etc., notably output buffering.
Colin
I was wondering what was going on here in Chris' code.:)
output buffering
I was wondering what was going on here in Chris' code.:)
return ob_get_clean();
No worries. Output buffering is a nice way to build strings for return using the output functions (echo / printf etc). I tend to find it's a cleaner way of handling it rather than trying to concatenate strings into a variable.
It creates a range for your days to add to the date and the extra argument $desc determines how that range is sorted, and in which order the days are added to the day.
Open in new window
Here's some sample output.
Function call: DateDropDown(0,8,"Dropdown
Output:
Open in new window
Function call: DateDropDown(0,8,"DropDown
Output:
Open in new window