Link to home
Start Free TrialLog in
Avatar of Colin Brazier
Colin BrazierFlag 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

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Norie
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

Avatar of Colin Brazier

ASKER

Thanks Chris, will check it out tomorrow.
and Norie!
Thanks guys.  Especially Chris who introduced me to some different concepts etc., notably output buffering.
Colin

output buffering

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

Open in new window

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.