2013-04-27 62 views
0

我已经使用了使用类似的代码回路和here在PHP尝试:我需要用php/javascript循环一段HTML代码,建议吗?

$options = ''; 

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

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


print "$Select"; 

,但没有运气...

编辑 这些例子都是伟大的,谢谢你们。 这就是我想要重复

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

$循环是牵强的mysql列的值。正如你可能看到的,我试图遍历一个列表x的元素(其中x = sql查询的行数)。

我以为试图把结果放入一个数组然后循环访问数组,但是我仍然无法让HTML代码相应地通过解析器而不被视为字符串。

+0

是否有必要的,你想用定界符? – 2013-04-27 10:38:54

+2

解释器在你的heredoc声明后抱怨空白,否则它没事 - [测试](http://codepad.org/sAkIjWPV) – Emissary 2013-04-27 10:40:00

+0

根据你发布的更新,试试这个然后[回声mysqli查询](http: //stackoverflow.com/questions/8806998/echoing-mysqli-queries) – Tigger 2013-04-27 11:29:32

回答

4

这个怎么样?

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

你可以尝试这样的事:

<?php 

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

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

?> 
0

here文档有自己的位置,但国际海事组织他们是被高估,并在错误的情况下经常使用。

试试这个:

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