Originally posted by jduk
hi devinemke,
I am sorry, the code you provided will not fit in to my code.
I am calling this function from a php file, the code was as I mentioned above. I am calling the variable in to this HTML .
<select name="start_year">$startyears</select>
So, can you please help me in modifing the function please.
Thnaks,
Well, let's think this through logically (an ability to do this is very useful when programming).
devinemke wrote
$year = date ('Y');
$years = range (1998, $year);
echo '<select name="exp_year">';
foreach ($years as $value) {echo '<option>' . $value;}
echo '</select>';
And you want something that would fit in
<select name="start_year"><?php echo $startyears?></select>
(or however exactly you want to do it - what you wrote isn't exact).
So you don't need the <select> or </select> tags from devinemke's code, because they're already provided for:
$year = date ('Y');
$years = range (1998, $year);
foreach ($years as $value) {echo '<option>' . $value;}
And you don't want to echo the <option> entities immediately either, but put them in a string. So let's make a string and put them in there.
$year = date ('Y');
$years = range (1998, $year);
$startyears = '';
foreach ($years as $value) {$startyears.= '<option>' . $value;}
You don't need to go with ranges or arrays, either. You could just use a for() loop.
$this_year = date ('Y');
$startyears = '';
for($year = 1998; $year<=$this_year; $year++) {$startyears.= '<option>' . $year;}
which is starting to look a lot like the code you started with, only starting from where you wanted and ending where you wanted.