0

I've attempted using a for loop and heredoc in php using similar code:

 $options = ''; 

for($Year = date("Y"); $Year <= date("Y") + 5; $Year++) 
{ $options .= "<option>$Year</option>\n"; } 

$Select = <<<QQxQQ 
<select> 
$options 
</select> 
QQxQQ; 


print "$Select";  

But no luck...

EDIT These examples are great, thanks guys. This is what I'm trying to iterate

<li><a href="#"><span>$looped</span></a></li>

Where $looped is a value of a fetched mysql column. As you can probably see, I'm trying to iterate an element of a list x amount of times (where x = number of rows of sql query).

I thought trying to put the results into an array and then cycling through the array, however I still couldn't get the HTML code to go through the parser accordingly without being considered a string.

3
  • Is it necessary that you want to use heredoc? Commented Apr 27, 2013 at 10:38
  • 2
    The interpreter is complaining about a whitespace after your heredoc declaration, otherwise it's fine - test Commented Apr 27, 2013 at 10:40
  • Based on the update you posted, try this then Echoing mysqli queries Commented Apr 27, 2013 at 11:29

4 Answers 4

4

How about this?

<select>
<?php
    for($Year = date("Y"); $Year <= date("Y") + 5; $Year++) {
        echo "<option>".$Year."</option>";
    }
?>
</select>
Sign up to request clarification or add additional context in comments.

Comments

0

You could try something like this:

<?php

$year = date('Y');
$end = ($year + 5);
do {
    $option .= '<option>'.$year.'</option>';
} while (++$year <= $end);

echo '<select>'.$option.'</select>';

?>

Comments

0

Heredocs have their place, but imo they are overrated and often used in the wrong context.

Try this:

$year = date("Y");
for( $Year = $year; $Year <= ($year+5); $Year++ ) { 
    $options .= "<option value=\"$Year\">$Year</option>\n"; 
}

echo "<select name='years' id='years'>";
echo $options;
echo "</select>";

Comments

0
<select>
<?php
for($Year = date("Y"); $Year <= date("Y") + 5; $Year++) 
{ echo "<option>$Year</option>\n"; } 
?>
</select> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.