Avatar of Colin Brazier
Colin Brazier
Flag for United Kingdom of Great Britain and Northern Ireland 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;
}

Open in new window

PHP

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Norie

Perhaps something like this.

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.
function DateDropDown($startdays,$size,$DateDropName,$SatSun, $desc=True) 
{
   // $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
   
  $days = range($startdays, $startdays+$size);

  if($desc){
    rsort($days);
  }

  echo "<select class='formSelect' id='$DateDropName' name='$DateDropName' >\n";

   foreach($days as $i)
   {
      $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;
}

Open in new window


Here's some sample output.

Function call: DateDropDown(0,8,"Dropdown","SatSun", False);

Output:
<select class='formSelect' id='Dropdown' name='Dropdown' >
<option value="2019-12-04">Wed Dec 4, 2019</option>
<option value="2019-12-05">Thu Dec 5, 2019</option>
<option value="2019-12-06">Fri Dec 6, 2019</option>
<option value="2019-12-07">Sat Dec 7, 2019</option>
<option value="2019-12-08">Sun Dec 8, 2019</option>
<option value="2019-12-09">Mon Dec 9, 2019</option>
<option value="2019-12-10">Tue Dec 10, 2019</option>
<option value="2019-12-11">Wed Dec 11, 2019</option>
<option value="2019-12-12">Thu Dec 12, 2019</option>
</select>

Open in new window


Function call: DateDropDown(0,8,"DropDown","SatSun", True);
Output:
<select class='formSelect' id='DropDown' name='DropDown' >
<option value="2019-12-12">Thu Dec 12, 2019</option>
<option value="2019-12-11">Wed Dec 11, 2019</option>
<option value="2019-12-10">Tue Dec 10, 2019</option>
<option value="2019-12-09">Mon Dec 9, 2019</option>
<option value="2019-12-08">Sun Dec 8, 2019</option>
<option value="2019-12-07">Sat Dec 7, 2019</option>
<option value="2019-12-06">Fri Dec 6, 2019</option>
<option value="2019-12-05">Thu Dec 5, 2019</option>
<option value="2019-12-04">Wed Dec 4, 2019</option>
</select>

Open in new window

Colin Brazier

ASKER
Thanks Chris, will check it out tomorrow.
Colin Brazier

ASKER
and Norie!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Colin Brazier

ASKER
Thanks guys.  Especially Chris who introduced me to some different concepts etc., notably output buffering.
Norie

Colin

output buffering

I was wondering what was going on here in Chris' code.:)
 return ob_get_clean();

Open in new window

Chris Stanyon

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.