Reputation: 6365
Can you please help me in getting data out of this for loop. It actually should print the names of 3 months, which it does if i dont use the $ period variable in there and use echo instead. But I want to put the value into the $period variable, so I can use it else where in my script. Right now, it print out only one month.
for ($i=1; $i<=3; $i++)
{
$period = '<option value="">';
$period .= date('F, Y', strtotime('+'.$i.' month'));
$period .= '</option>';
}
echo $period;
Upvotes: 0
Views: 82
Reputation: 484
Try this:
$numPeriods = 3;
$period = '';
for($i=1; $i<=$numPeriods; $i++)
{
$period .= '<option value="'.$i.'">';
$period .= date('F, Y', strtotime('+'.$i .' month'));
$period .= '</option>';
}
echo $period;
Upvotes: 1
Reputation: 33502
you need to output the value of your $period
with each iteration.
for ($i=1; $i<=3; $i++)
{
$period = '<option value="">';
$period .= date('F, Y', strtotime('+'.$i.' month'));
$period .= '</option>';
echo $period;
}
Upvotes: 3
Reputation: 3414
You're overwriting the value of $period on each iteration. Try the code below:
$period = "";
for ($i=1; $i<=3; $i++)
{
$period .= '<option value="">';
$period .= date('F, Y', strtotime('+'.$i.' month'));
$period .= '</option>';
}
echo $period;
Upvotes: 6